thread safe class

http://stackoverflow.com/questions/28005734/making-a-class-thread-safe-in-ios

Thread safety means that the data structure can be accessed and/or modified by multiple threads without becoming corrupt.

One simple approach is to use Objective-C’s @synchronized capability.

In this case, @synchronized(self.data) around all of your accesses to the array will ensure that only a single thread can access the array at a time.

Even though length doesn’t modify the array, you still need to protect its access because another thread could potentially modify the array –

Better Solution

Instead of using synchronize, we can use serial queues.

1) Create private property

get: self.privateFoo
set: self.privateFoo = someObject

2) Create custom set and get method.

Use a serial queue in there to access the private property. Get should be async. Set should be sync.

Thread Safety in ios