ios - singleton, thread safe setters -
ios - singleton, thread safe setters -
i have singleton class ensure singleton doing
static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{
as documented before. need add together
nsmutabledictionary *selections
property singleton other parts of app update ( adding , reading key/value pairs). question how create sure property thread safe? declare atomic? need create it's setters , getters thread safe !. recent objective c programmer coming on c#/ java background, why bit confused issue.
thanks
a thread-safe , exception-safe way utilize @synchronized
perform locking
@property (readonly) nsmutabledictionary *selections; - (void)withselection:(void (^)(nsmutabledictionary *))callback { if (!callback) return; @synchronized(self.selection) { // need create sure self.selection isn't nil callback(self.selection); } }
__block nsdictionary *copiedselection; [[singleton singleton] withselection:^(nsmutabledictionary *selection) { // can modify selection safely here copiedselection= [selection copy]; }]; // can utilize copiedselection here
you can utilize gcd dispatch_queue serialize operation on selection
ios objective-c ios7
Comments
Post a Comment