当你需要更新UI并在viewDidLoad中调用时,你如何处理Parse调用主线程?

时间:2022-08-11 20:58:27

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?

我真的在努力应对多线程概念。所有对Parse的调用都是在主线程上完成的,我理解这是因为你不想在等待回复时阻塞主线程。但是,如果我需要在显示屏幕之前从解析中获取数据以更新我的UI,该怎么办?

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

我已经将PFUser子类化以添加其他字段,因此我想获取这些附加字段并使用数据填充屏幕,以便用户可以修改其信息

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")

}

1 个解决方案

#1


0  

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.

在显示视图之前调用此方法,并将任何更改写入dispatch_get_main_queue()块内的显示,以便您的更改立即反映在UI上。您已经设置了用户对象,没有写入UILabel或者用户名或其他内容。

#1


0  

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.

在显示视图之前调用此方法,并将任何更改写入dispatch_get_main_queue()块内的显示,以便您的更改立即反映在UI上。您已经设置了用户对象,没有写入UILabel或者用户名或其他内容。