I'm really struggling with the multi-threading concept. All calls to Parse are done off the main thread which I understand is because you don't want to block the main thread while waiting for a reply. But what do I do if I need to get data from parse to update my UI before the screen is displayed?
I have subclassed PFUser to add additional fields so I want to get those additional fields and populate a the screen with the data so the user can modify their info
func getCurrentUser () -> ParseUser? {
let currentUser = PFUser.currentUser()
var user: ParseUser?
let query = PFQuery(className:"_User")
query.whereKey("objectId", equalTo: (currentUser.objectId!))
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
dispatch_async(dispatch_get_main_queue()){
if error == nil {
for object in objects! {
user = object as! ParseUser
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
}
}
return user
print("user returned")
}
call this method before you display your view and write any changes to the display inside your dispatch_get_main_queue() block so your changes are reflected on the UI immediately. You've set your user object, no write to a UILabel or something with your User's name or whatever.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.