user3480091 user3480091 - 7 months ago 16
Swift Question

How do I use my parse retrieved object outside of query function

How do I make it so that my parse data can be shown where I want it?

I understand that findObjectsInBackgroundWithBlock is asynchronous.

That does not tell me how to make use of my retrieved object outside of the query function.

class ChoosePersonViewController: UIViewController, MDCSwipeToChooseDelegate {

var people:[Person] = []

// I want this variable to be updated with the parse object and be able to call it in the dafaultPeople() function
var nameData = ""

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.people = defaultPeople()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.people = defaultPeople()
// Here you can init your properties
}


override func viewDidLoad(){
super.viewDidLoad()
let userObjectID = PFUser.currentUser()!.objectId!
let query = PFQuery(className:"Restaurants")
query.whereKey("Pointer", equalTo: userObjectID)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in

if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
var nameData = object.objectForKey("Name") as! String
self.people.append(Person(name: nameData, image: nil, age: nil, sharedFriends: nil, sharedInterest: nil, photos: nil))

// It successfully displays the data here
print(object.objectId, nameData)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}

func defaultPeople() -> [Person]{
// nameData is blank here
return [Person(name: nameData, image: UIImage(named: "finn"), age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5)]

}


Person.swift

import UIKit

class Person: NSObject {

let Name: NSString
let Image: UIImage!
let Age: NSNumber
let NumberOfSharedFriends: NSNumber?
let NumberOfSharedInterests: NSNumber
let NumberOfPhotos: NSNumber

override var description: String {
return "Name: \(Name), \n Image: \(Image), \n Age: \(Age) \n NumberOfSharedFriends: \(NumberOfSharedFriends) \n NumberOfSharedInterests: \(NumberOfSharedInterests) \n NumberOfPhotos/: \(NumberOfPhotos)"
}

init(name: NSString?, image: UIImage?, age: NSNumber?, sharedFriends: NSNumber?, sharedInterest: NSNumber?, photos:NSNumber?) {
self.Name = name ?? ""
self.Image = image
self.Age = age ?? 0
self.NumberOfSharedFriends = sharedFriends ?? 0
self.NumberOfSharedInterests = sharedInterest ?? 0
self.NumberOfPhotos = photos ?? 0
}
}

Answer

In your query, add your objects to your people:

for object in objects {
    var nameData = object.objectForKey("Name") as! String                  
    self.people.append(Person(name: nameData, image: nil, age: nil, sharedFriends: nil, sharedInterest: nil, photos: nil))
}

Currently I put everything besides the name as nil because I am not sure which keys you are using to retrieve those. However, the general idea would be the same.