0
votes
let userQuery = PFUser.query()
userQuery!.whereKey("username", equalTo: userUsername)
userQuery!.findObjectsInBackgroundWithBlock({ (result:[PFObject]?, error:NSError?) in
    currentProfilePageUser = result![0] as! PFUser
 })

This is my code. I'm trying to get a PFUser with only the username. But, I added breakpoints inside findObjectsInBackgroundWithBlock and discovered that it is not getting called. Therefore, I cannot receive my data. Why isn't findObjectsInBackgroundWithBlock getting called?

let query = PFQuery(className: "Follow")
    query.whereKey("followFrom", equalTo: PFUser.currentUser()!.objectId!)
    query.whereKey("followingTo", equalTo: currentProfilePageUser)
    query.findObjectsInBackgroundWithBlock { (result:[PFObject]?, error:NSError?) in
        if result == nil{
            print("currentuser is not following this user")
            self.followButtonTitle.setTitle("Follow", forState: .Normal)
        }else{
            print("currentuser is follwing this user")
            self.followButtonTitle.setTitle("Unfollow", forState: .Normal)
        }
    }

For some really weird reason, the first findObjectsInBackgroundWithBlock works now, but this other one doesn't get called. This is very weird.

2
It is probably being called, but since it is "inBackground" this means that it is called on a background thread. Try testing for error and add a print statement for them both. You should see that at least one of them are called then. - Marius Waldal

2 Answers

0
votes

Try this and see if you get a print in your console:

let userQuery = PFUser.query()
userQuery!.whereKey("username", equalTo: userUsername)
userQuery!.findObjectsInBackgroundWithBlock({ (result:[PFObject]?, error:NSError?) in
    if(error == nil){
        print("Success!")
    } else {
        print("Failed with error \(error)")
    }
})
0
votes

The 2nd query i posted above requires the first query's results. For some reason, the 2nd query runs first, then the first regardless of the order of my code. My fix was to put the 2nd query inside viewDidAppear, putting it in viewWillAppear won't even work.