Avoid copying NSMutableArray for reading with multithreaded writes

I have a class that uses a modified array, which is modified once after a large number of readings (new elements appear).

The problem is that when it comes time to mutate the array, reading continues to flow.

Currently, in order to avoid this problem every time she reads something, she does it on the copy:

[[theArray copy] operation] //operation being indexOfObject:, objectAtIndex: objectsAtIndexes:, etc.

A copy becomes very expensive, especially when there is no need (all those times when the array does not mutate).

How to block an array to delay access to it when it mutates?

+5
source share
3 answers

. . . " " Concurrency.

iOS >= 4.3, . , , , . , , - , , . ( GCD , .) .

+9

- @synchronized, :

-(void) accessTheArray {
    MyClass *obj;
    @synchronized(theArray) {
        obj = [theArray objectAtIndex:...];
    }
    [obj someMessage];
}

: ARC, / , ( ) , someMessage ( omz ).

+5

/. Cocoa , pthread_rwlock_t pthreads - pthread.h. , ( ), @synchronized, .

+2

All Articles