objective c - Obj-c is NSMutablearray thread safe in this Queue, and does reading it needs to be queued also? -


i'm working on ios app in xcode 7, , have had problems thread safety of global nsmutablearray read many times updates on tableview.

because of strange chrashes, tried adding queue updating of global array (global view), array updated database function call (written class method other global functions). update called main threads other calls coming background threads , timer events.

i add globals:

nsmutablearray *mymutarray ; dispatch_queue_t the_queue ; 

then in viewdidappear

the_queue = dispatch_queue_create("my_queue", dispatch_queue_serial) ; 

and function updating array (from classinstance)

-(void)update_array {      dispatch_sync("my_queue"),^{          mymutarray = [[nsmutablearray alloc] initwitharray:[globalfunction updatearrayfromdb] ] ;      } } 

with hoping avoid thread problems, necessary have read function like:

-(nsarray*)read_array{    __block nsarray *array ;    dispatch_sync("my_queue",^{          array = [nsarray arraywitharray:mymutarray ] ;     });     return array; } 

or not necessary if reading of array performed on main thread time? also, dispatch_sync block run on main thread, or should instead call function make them run on main thread, methods don't need run in background, still can called several locations. thinking if using nsoperationqueue better this:

-(void)update_array {     [[nsoperationqueue mainqueue] addoperationwithblock:^{         mymutarray = [[nsmutablearray alloc] initwitharray:[globalfunction updatearrayfromdb] ] ;  }); ) 

or give different result? main goal blocks run on main thread, in order called.

and 1 thing i'm confused this, these blocks (my dispatch methods , nsoperation method) called after entire method/block has finished on main thread (f.ex after -(void) 20 lines of code has run finish) or started inside method/block, after line of code has executed, more code lines waiting. (since thing trying avoid thread safety, know when threads execute?

also there way avoid same block gets called several times in row if added many times, when stacked this:

call read call update <- if 1 progress call read call read <- these returns same unnecessary , waste time call read <- these returns same unnecessary , waste time call read <- these returns same unnecessary , waste time 

nsmutablearray not thread safe; not simultaneous writing , not simultaneous reading, either.

if have mutable array spans threads, need serialize reads , writes. easiest way serial queue dispatch_sync() operations (like in code provided).

there no need operations on main queue (and, in fact, doing lead responsiveness issues).

alternatively, if writing infrequent or ok data displayed user (or otherwise) lag slightly, consider calling copy on nsmutablearray create nsarray instance thread safe.


the main queue serial. 1 block @ time can processed on , entire block must execute , return before block executed.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -