This is a fundamental problem with executing asynchronous blocks, and I haven't found a good solution yet.
The following code works, but it blocks the main thread, and I'd like to avoid that.
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
//check if transaction already exists
let trans = PFQuery(className: "Transactions")
trans.whereKey("itemId", equalTo: self.pfObject.objectId!)
//I need the count of objects in the query before I can proceed, but I don't want to block the main thread
let count = trans.countObjects()
if(count > 0){
return false
}else{
return true
}
}
This problem isn't specific to this part of my application. Normally, I can just set "count" (or whatever variable I need) in the closure of something like query.countObjectsInBackGroundWithBlock(), but I can't do that when I need to return something on the main thread.
Is there a solution to make my application wait for return without blocking the main thread? I actually don't think that there is in this case without redesigning a large portion of code, but I just want to make sure I'm not being naive.
What are the accepted solutions for these types of problems?