adit
adit

Reputation: 33644

locking and synchronized issues

I have the following method:

-(void)populateSpotVenueIndex
{
     @synchronized(self.spotsResults) {
    [self.pollVenueIndex removeAllObjects];
    for (PFObject * poll in self.spotsResults)
    {
        NSString * venue =  [((PFObject *)[poll objectForKey:@"parent"]) objectForKey:@"name"];
        [self.pollVenueIndex setObject:venue forKey:[poll objectForKey:@"question"]];
        dispatch_async(dispatch_get_main_queue(), ^{
          [self.tableView reloadData];
        });
    }
}

}

which gives me:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x3ec0d0> was mutated while being enumerated.'
*** First throw call stack:
(0x328fc8bf 0x367661e5 0x328fc3e3 0x80eb 0x8787 0x371aed55 0x371ba7a3 0x36e221cf 0x36e220a4)

How is this even possible? Basically what I want to achieve here is that if the loop is still executed and this populateSpotVenueIndex is called I just wanted to cancel this looping.. is there such way to do that?

Upvotes: 0

Views: 215

Answers (3)

morningstar
morningstar

Reputation: 9122

Do you make sure that everywhere else you modify self.spotsResults you also use @synchronized(self.spotsResults)? Particularly if you modify it in any code that gets called from reloadData (like cellForRowAtIndexPath). It would be unusual I would think to modify an array in reloadData, but that's my guess.

Upvotes: 0

bryanmac
bryanmac

Reputation: 39296

Collection <__NSArrayM: 0x3ec0d0> was mutated while being enumerated

It means your changing the collections data while you're enumerating.

I don't see you directly changing spotResults inside the loop. Is it possible it's being indirectly changed as a result of tableView reloadData in one of the tableView callbacks?

Also, why update the table view inside the loop? Update your data in the loop and then call to reloadData ...

Upvotes: 0

morningstar
morningstar

Reputation: 9122

I doubt it has anything to do with threading or synchronization. It just means you tried to do something inside the loop that changed the contents of the array, like addObject or removeObject.

Post the code for the body of the loop if that doesn't lead you to the solution.

Upvotes: 2

Related Questions