开发者

issue with multithreading/GCD and using synthesizes

开发者 https://www.devze.com 2023-04-12 15:29 出处:网络
So I have the following call everytime the button is clicked: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

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:

issue with multithreading/GCD and using synthesizes

When I give it some time for that thread to finish and call it again it gives me this:

issue with multithreading/GCD and using synthesizes

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....)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号