0

How can I wait until data is retrieved from parse.com?

This is the function I have that returns an empty string since the response from parse.com is too slow. If I put a breakpoint inside the success area it will break "long" after the data is needed. I guess there is a way to get the data synchronous so it will wait?

func getObjectId(localPersonId:NSString) -> NSString{
    var currentObjectId:NSString = ""

    var query = PFQuery(className:"myClass")
    query.whereKey("personId", equalTo:localPersonId)
    query.whereKey("groupId", equalTo:self.currentGroupId)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            // should not use a for loop since this should 
            // only return one row
            for object in objects {
                currentObjectId = object["objectId"] as NSString
            }
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }

    return currentObjectId
}

In this case the getObjectId function will return an empty string. Anyone?

7
  • You'll need to make your getObjectId function asynchronous. Right now the line return currentObjectId is being executed before the query has finished. Commented Feb 23, 2015 at 13:45
  • Yes, that is the problem. How can I make the function asynchronous? Commented Feb 23, 2015 at 14:35
  • See here stackoverflow.com/questions/25203556/… Commented Feb 23, 2015 at 15:50
  • Sadly, that link did not make me see the light with my issue... I thought I maybe should use something other than findObjectsInBackgroundWithBlock. Not sure. Too much of a newbie to know what is best approach for this. Commented Feb 23, 2015 at 16:47
  • You won't be able to avoid using one of the Async methods. If you were using Objective-C I could write you up an answer, unfortunately I'm not up to scratch with Swift yet. Have you looked at the tutorials on the Parse website, perhaps they have updated one of their iOS example/tutorial apps to use Swift? Commented Feb 24, 2015 at 3:00

1 Answer 1

2

I realize this is 3 months old but although the Parse docs are incredibly good/useful, there isn't a whole lot out there answering IOS Parse related questions.

This should work. It uses a completion handler, which is a simple way of dealing with this issue.

(for more on completion handlers in asynch context: https://thatthinginswift.com/completion-handlers/ )

func getObjectId(localPersonId:NSString, completionHandler: (currentObjectId: [String]) -> ()){

    var currentObjectId:NSString = ""

    var query = PFQuery(className:"myClass")
    query.whereKey("personId", equalTo:localPersonId)
    //query.whereKey("groupId", equalTo:self.currentGroupId)
    query.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in
        if error == nil {
            // should not use a for loop since this should
            // only return one row
            for object in objects {
                completionHandler(currentObjectId: currentObjectId)
            }
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error!, error!.userInfo!)
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I while since I had this problem. I have probably done a hack for this to work since I got no response, but I'll have a look at my old code and have a look. Anyway; great to learn about the completionHandler :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.