Error with Parse Query Findobjectsinbackgroundwithblock

Error with Parse Query findObjectsInBackgroundWithBlock

Try changing [AnyObject]? to [PFObject]?. This seems to be required by Swift 2.0.

So instead of:

findTimelineData.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error:NSError?) -> Void in

Use:

findTimelineData.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in

You'll also need to change your iteration over the array objects, since they will now already be PFObject.

Error with Parse findObjectsInBackgroundWithBlock in Swift

You need to do the following:
declare this:

var array:NSArray = NSArray()

query.findObjectsInBackgroundWithBlock {(objects:PFObject?, error:NSError?) -> Void in

if error != nil {
print("error")
} else {
if let objects = objects{
for object in objects {

self.append(object)

}

}

}

}

Parse Server // findObjectsInBackgroundWithBlock // Array

You need to use closures with your function because you are making block and block will work in async manner, so that you need to declare your function with closure and then return the String array with that closure.

func userKcal(completion: ([String]) -> ()) {
let query = PFQuery(className: "StringClassInParse")
query.whereKey("userPointer", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) in
if let returnedObjects = objects {
for object in returnedObjects {
let kcal = object["kcal"] as? String
self.strings.append(kcal!)
}
}
completion(self.strings)
}
}

Now call this function like this way

self.userKcal { (strings) -> () in
print(strings)
}

Query ParseUser Swift 3 Error

This is where the meat of the error message lies:

Cannot convert value of type '([PFObject]?, NSError?) -> Void' to expected argument type '([PFObject]?, Error?) -> Void?

Swift 3.0 introduced the Error protocol, which is what is catching you here.

Try the following change and see if it resolves your problem:

findUser.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error: Error?) -> Void in // Changes NSError to Error
if error == nil{
let user:PFUser = (objects as NSArray).lastObject as PFUser
cell.usernameLabel.text = user.username

UIView.animateWithDuration(0.5, animations: {
cell.typeOfPartyLabel.alpha = 1
cell.timeOfPartyLabel.alpha = 1
cell.usernameLabel.alpha = 1
})
}
}

return cell
}

Simple Parse.com with findObjectsInBackgroundWithBlock does not return

The problem was when the method was called. The numberOfRowsInSection was not was passing a nil value.

findObjectsInBackgroundWithBlock: gets data from Parse, but data only exists inside the block

The last NSLog(@"The dictionary is %@", self.scoreDictionary) statement does not actually execute after the block completes. It executes after the findObjectsInBackgroundWithBlock method returns. findObjectsInBackgroundWithBlock presumably runs something in a separate thread, and your block may not actually execute at all until some length of time after that last NSLog statement. Graphically, something like this is probably happening:

Thread 1 
--------
retriveDataFromParse called
invoke findObjectsInBackgroundWithBlock
findObjectsInBackgroundWithBlock queues up work on another thread
findObjectsInBackgroundWithBlock returns immediately |
NSLog statement - self.scoreDictionary not yet updated |
retriveDataFromParse returns |
. V
. Thread 2, starting X milliseconds later
. --------
. findObjectsInBackgroundWithBlock does some work
. your block is called
. for-loop in your block
. Now self.scoreDictionary has some data
. NSLog statement inside your block

You probably want to think about, what do you want to do with your scoreDictionary data after you have retrieved it? For example, do you want to update the UI, call some other method, etc.? You will want to do this inside your block, at which point you know the data has been successfully retrieved. For example, if you had a table view you wanted to reload, you could do this:

for (PFObject *object in objects){
....
}
dispatch_async(dispatch_get_main_queue(), ^{
[self updateMyUserInterfaceOrSomething];
});

Note the dispatch_async - if the work you need to do after updating your data involves changing the UI, you'll want that to run on the main thread.



Related Topics



Leave a reply



Submit