So I have the following call everytime the button is clicked:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self populateSpotVenueIndex];
});
-(void)populateSpotVenueIndex
{
@synchronized(self.spotsResults) {
[self.pollVenueIndex removeAllObjects];
PFQuery * query = [PFQuery queryWithClassName:@"Venue"];
for (PFObjec开发者_JS百科t * poll in self.spotsResults)
{
[query getObjectInBackgroundWithId:((PFObject *)[poll objectForKey:@"parent"]).objectId block:^(PFObject * object, NSError * error){
if (!error && [object objectForKey:@"name"] && [poll objectForKey:@"question"]) {
[self.pollVenueIndex setObject:[object objectForKey:@"name"] forKey:[poll objectForKey:@"question"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
//[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else
NSLog(@"Error is %@", [error userInfo]);
}];
}
}
}
When I am calling this for the first time it is fine, but calling it the second time it crashes with the following error:
When I give it some time for that thread to finish and call it again it gives me this:
I believe that this is because I am trying to use self.spotsResults which is currently used by the other thread... so how do I resolve this?
You [object objectForKey:@"name"] return nil for some reason. Better is using little trick:
id objectToSet = [object objectForKey:@"name"];
if (objectToSet) {
[self.pollVenueIndex setObject:objectToSet forKey:[poll objectForKey:@"question"]];
} else NSLog(@"wow, object is nil for: (some info for debug....)
精彩评论