简体   繁体   中英

Swift Parse - Method doesn't stop executing

I have the following code which basically would query Parse class and return a result set. After returning the results, I would pass that array to a function to check if some elements are set or not.

I used print statements all over the code to try and debug, and found that query executes and within the

if error == nil

I am getting the results array to be empty. Hence when I pass that to my function below, it never gets out of it:

func emailOrUsernameIsTaken(results: [PFObject])->Int
    {
        /*Check if username is taken or if email is taken*/

        var preferenceTaken: Int = 0

        if(results[0]["email"] as! String != "" && results[0]["email"] as! String == self.userObject.email!)
        {
            preferenceTaken = 1
        }else if(results[0]["appUsername"] as! String != "" && results[0]["appUsername"] as! String == self.userObject.username!){
            preferenceTaken = 2
        }

        return preferenceTaken
    }

and this is the code where the query is taking place:

let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
        query.findObjectsInBackgroundWithBlock {
            (results: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                print("Before")
                let checkResult = self.emailOrUsernameIsTaken(results!)
                print(results)
                print("After")
            }
        }

as an output from print statements above, I get in console:

Before
Optional([])
After

Can someone help me to find out the issue. I am surprised why this is not working.

If results is empty, results[0]["email"] as! String results[0]["email"] as! String should crash because you are force unwrapping a nil optional value. So the story you are telling is not consistent.

If the actual problem is the empty results - you would have to provide details about your queries and why you expect there to be a populated result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM