I have a private variable in my class:
BOOL isEnabled;
and a method:
-(void) foo {
if(isEnabled) {
// do some operations
}
else {
// wait till other thread makes isEnabled = TRUE and do operations
}
}
I need to synchnize the method somehow. After isEnabled becomes true I can do my operations.
Your question is quite general and it’s difficult to answer without some minimal example.
If you want to sync the execution of different thread I suggest you to read about
NSOperationandNSOperationQueue.NSOperationQueues handle the synchronization for free.Simply speaking, a
NSOperationis an operation that is executed in background. Each operation is executed within a queue (NSOperationQueue).Within an
NSOperationQueueyou can specify the number of concurrent operation you can run in background.For example, the code above set the operations that can be executed to 1. For shared resources, this the same to sync the access to that resource among different operations.
In addition, you can set dependencies among operations.
The code means that
operation1must finish beforeoperation2can run.For further info I suggest the following tutorial on nsoperation-and-nsoperationqueue. Of course you can also see class references on Apple doc.
Hope it helps.