关于所有UI工作的东西必须在swift的主线程上完成?

时间:2015-03-09 02:21:28

标签: ios multithreading swift parse-platform

我正在学习使用Swift与Parse进行通信。我在这里发现了一些疑问。 以下是代码的两个分段,其中我使用Parse来查找关联用户及其头像。第一个工作正常,但我想我应该把所有UI工作放在主线程上,所以我把它改成第二个。现在,我想知道这样做是否有不正确之处?

findSweeter.findObjectsInBackgroundWithBlock {
    (objects:[AnyObject]!, error:NSError!)->Void in
    if error == nil {
        let user:PFUser = (objects as NSArray).lastObject as PFUser
        cell.usernameLabel.text = user.username

        let profileImage:PFFile = user["profileImage"] as PFFile
        profileImage.getDataInBackgroundWithBlock {
            (imageData:NSData!, error:NSError!)->Void in

            if error == nil {
                let image:UIImage = UIImage(data: imageData)
                cell.profileImageView.image = image
            }
        }
    }
}


findSweeter.findObjectsInBackgroundWithBlock {
    (objects:[AnyObject]!, error:NSError!) -> Void in
    if error == nil {
        let user:PFUser = (objects as NSArray).lastObject as PFUser

        let profileImage:PFFile = user["profileImage"]? as PFFile
        profileImage.getDataInBackgroundWithBlock { (imageData:NSData!, error:NSError!)->Void in
            if error == nil {
                let image:UIImage = UIImage(data: imageData)!

                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    cell.usernameLabel.text = user.username
                    cell.profileImageView.image = image
                })

            }
        }
    }
}

0 个答案:

没有答案