ios - NSMutableArray Thread Concurrency with GCD -
i have nsmutablearray in "sharedstore"-pattern singleton.
publicly, it's accessible through methods cast nsarray. within class, it's
@property (nonatomic, copy) nsmutablearray *myitems;
this array never gets manipulated outsdie singleton viewcontrollers send singleton messages manipulate controller. of these messages empty array, re-populate it, etc.
having ended in situation array empty in 1 method call , not yet empty in next, i've started implementing concurrency behaviour.
here's i'm doing far:
in .m file of singleton, have
@property (nonatomic, strong) dispatch_queue_t arrayaccessqueue;
in singleton's initializer gets created serial queue. , then, every method has mutating array within dispatch_sync
call, example:
dispatch_sync(self.arrayaccessqueue, ^{ [_myitems removeallobjects]; });
this has made things better , has made app behave more smoothly. however, have no way of quantifying beyond having fixed 1 odd behaviour described above. kind of feel i'm in dark problems may lurking beneath surface.
this pattern makes sense me, should using else, @synchronize
or nslock
or nsoperationqueue
? come bite me?
using dispatch_sync
fine long wrap array reads , writes , ensure serial queue.
but improve things allowing concurrent reads. this, use dispatch_sync
around array reads , use dispatch_barrier_sync
around array writes. , setup queue concurrent.
do ensures single write can happen @ time, reads block until write done, , write wait until current reads done.
Comments
Post a Comment