I'm pretty new to multithreaded programming & cocoa.
I'm going to need a lock to access/modify a NSMutableArray
iVar. What's the simpliest way to do it ?
And while I'm here, do you guys have some reading about multithreaded programming with Obj-c/Cocoa ? Thx.
Using the @synchronized
block:
@synchronized(MyArray)
{
// thread safe code
}
This block is not recognized by Xcode at times, but trust me, it works!
For more information, you can read this article.
An alternative method is to use GCD and serial queues.
For your NSArray property, write accessors that use a private serial queue. to set, and fetch the values from the backing store. Since serial queues Are FIFO, and will run the blocks sequentially this provides thread safe access to the property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With