I will try to explain this as best as possible.
I use Parse.com and return data from the Parse database class. I want to put this call to parse.com in my own function in a custom class. The problem I am having is completion. Where is he going? I tried many different versions of adding it to my function, but it does not work.
Here is a function that takes a class name, table name, and sort descriptor and returns an array:
func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor) -> [Any]
When I add completion to it, I use (which may be wrong):
func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor, completion: (result: Any)->Void)
Now inside the function I use the Parse.com code to exit and get the data
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in if error == nil { // Do something with the found objects for object in objects { self.arrayOfObjects.append(object[parseObject]!) } } else { // Log details of the failure println("Error: \(error) \(error.userInfo!)") } }
My goal here is to send the parameters of my class function, get data from parse.com and then return the data as an array AFTER an asynchronous call
I try to call it this way:
myClass.queryDataInBackgroundWithBlock("databaseName", parseObject: "columnName", sortDescriptor: orderBy){ (result: Any) in println(result) }
It almost looks like a nested termination. How to return an array after its completion? Is it passed to a function that then returns it, or should it be returned in nested code, or what? It retrieves the data, but the problem is the completion of the AFTER response.
UPDATE: As I said in the comment below:
query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]!, error: NSError!) -> Void in if error == nil { // Do something with the found objects for object in objects { self.arrayOfObjects.append(object[parseObject]!) } } else { // Log details of the failure println("Error: \(error) \(error.userInfo!)") } }, completion: { //do something here })
This returns an error: βExtra completion of the argument in the callβ I'm not sure how to add the completion at the end of the block, so I added () around the block and inserted the completion. This is obviously wrong, but I'm not sure how to add completion at the end of the block, as matte suggested
UPDATE 2:
func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor) -> [Any]{ var query = PFQuery(className:parseClass) if sortDescriptor.key != "" { query.orderBySortDescriptor(sortDescriptor) } query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in if error == nil { // Do something with the found objects for object in objects { self.arrayOfObjects.append(object[parseObject]!!) } } else { // Log details of the failure println("Error: \(error) \(error.userInfo!)") } } return self.arrayOfObjects //<-- needs to move to completion }