Created
November 28, 2011 10:16
-
-
Save aquarius/1399872 to your computer and use it in GitHub Desktop.
What is the best way to get notified when all callbacks have returned?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (myObject in objects) { | |
[myObject doWithCallback: ^(BOOL success){ | |
// | |
}]; | |
} |
can you elaborate a bit more on the problem? I guess the callbacks spin of a new thread? You could consider a NSOperationQueue and listen to the case where it's empty again.
How do you listen to the "queue empty" event on a gdc queue?
__block NSUInteger counter = 0;
NSUInteger max = [objects count];
dispatch_queue_t q = dispatch_queue_create("com.mindnode.fafnerqueue", DISPATCH_QUEUE_SERIAL);
for (myObject in objects) {
[myObject doWithCallback: ^(BOOL success){
dispatch_async(q, ^{
if(++counter >= max) {
// Done!
dispatch_release(q);
}
}];
}
Alternative (probably more efficient):
__block volatile int64_t counter = 0;
int64_t max = (int64_t)[objects count];
for (myObject in objects) {
[myObject doWithCallback: ^(BOOL success){
if(OSAtomicIncrement64Barrier(&counter) >= max) {
// Done! Will only be executed once.
}
}];
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution:
GCD Dispatch Queues
http://mikeash.com/pyblog/friday-qa-2009-09-04-intro-to-grand-central-dispatch-part-ii-multi-core-performance.html