On the main thread, continuous work is performed. swift

In mine ProfileViewController, I have a request that retrieves a user profile image that is stored as a PF file.

  var query = PFQuery(className:"Users")
    query.whereKeyExists("profilePicture")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            self.userNameLabel.text = PFUser.currentUser().username

            if let imageFile = PFUser.currentUser().objectForKey("profilePicture") as? PFFile {
                if let data = imageFile.getData() {
                    self.profPic.image = UIImage(data: data)
                }
            }

        }
        else {
            println("User has not profile picture")
        }
    }

This is the only request in this view, I have another request on my home page of my application, in which there are all messages of all users. The error I get is s. A long-running operation is being executed on the main thread.Next followsBreak on warnBlockingOperationOnMainThread() to debug.

I don’t know how to get around this, because I need to make another request in order to get the current publication of users for this profile. Should I use something other than findObjectsInBackgroundWithBlock. Thank you

+4
source share
2 answers

Parse sdk. : imageFile.getData() , Parse , . getDataInBackground... . .

+5

@danh, , @danh!

override func viewDidLoad() {
    super.viewDidLoad()


    var query = PFQuery(className:"Users")
    query.whereKeyExists("profilePicture")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            self.userNameLabel.text = PFUser.currentUser().username

            if let imageFile = PFUser.currentUser().objectForKey("profilePicture") as? PFFile {
            imageFile.getDataInBackgroundWithBlock { (data: NSData!, error: NSError!) -> Void in
                    self.profPic.image = UIImage(data: data)
                }
            }

        }
        else {
            println("User has not profile picture")
        }
      }
    }
+3

Source: https://habr.com/ru/post/1569096/


All Articles