I have a class that uses a mutable array that is modified once after a lot of reads (new items arrive).
The problem is that when times comes to mutate the array, rea
The simplest approach is to use @synchronized, like this:
-(void) accessTheArray {
MyClass *obj;
@synchronized(theArray) {
obj = [theArray objectAtIndex:...];
}
[obj someMessage];
}
EDIT: If not using ARC, you might want to retain/autorelease the object, otherwise it might be removed from the array (and released) before someMessage
is called (thanks for omz for this excellent comment).