Async quick shutdown to return array to user class

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 } 
+5
source share
1 answer

Inside the queryDataInBackgroundWithBlock function queryDataInBackgroundWithBlock you get a completion block called completion . It takes one parameter. So, the last thing you do, after you have the data, calls it, passing the data to it:

 completion(result:myData) 

And since query.findObjectsInBackgroundWithBlock itself is asynchronous, you will need to make this call last in the query.findObjectsInBackgroundWithBlock block.

Like this:

 func queryDataInBackgroundWithBlock( parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor, completion: (result: Any)->Void) { 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!)") } completion(result:self.arrayOfObjects) } } 
+6
source

Source: https://habr.com/ru/post/1209195/


All Articles