Swift: Performing a segue if PFObject = nil.

306 views Asked by At

Currently I have synced user and their data on the parse backend. So what I want to do is to have user details page appear if the users have not stated their user details. So there will be the login page which will lead you to the protected page (which will show user details) and if there are no details to be retrieved, the page would perform a segue to the user details page.

var query = PFQuery(className:"UserSettings")
    var CurrentUser = PFUser.currentUser()
    query.whereKey("user", equalTo: CurrentUser!)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if objects == nil {
          self.performSegueWithIdentifier("fillUserDetails", sender: self)
        }

However, the segue does not seem to execute and it remains on the protected page rather than directing the new user to the user details page. Any help would be greatly appreciate and thanks in advance.

2

There are 2 answers

4
NehaK On

add check of object's size and null both because objects will null when any error occurs in request, otherwise object will not null but it can have 0 data.

swift -

if objects == nil || objects.size() == 0 {

          self.performSegueWithIdentifier("fillUserDetails", sender: self)

}
0
Bannings On

The block is executed on background thread. When you update your UI you should do that on the main thread. Try wrapping the performSegueWithIdentifier in a block like this:

var query = PFQuery(className:"UserSettings")
var CurrentUser = PFUser.currentUser()
query.whereKey("user", equalTo: CurrentUser!)
query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) -> Void in

    if objects == nil {
        dispatch_async(dispatch_get_main_queue()) {
            self.performSegueWithIdentifier("fillUserDetails", sender: self)
        }
    }