我正在学习使用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
})
}
}
}
}