-1

I'm using blocks inside a NSOperation. The main method of my NSOperation looks like this:

- (void) main {

    [self callMethodOfALiraryUsingCompletionBlock:^() {
       //this method takes time to execute, will call this block when done
    }];
}

I'd like my NSOperationto terminate when what's inside the block is done. Currently, it returns directly, before what's inside the block is executed ... Any idea how I can solve this ?

rmonjo
  • 2,675
  • 5
  • 30
  • 37
  • 1
    You are calling a method that takes a block, so what is "callMethodWithBlock" doing?. Perhaps you should just write the code you want to execute in the main method? – Lucas Derraugh Dec 10 '13 at 18:53
  • 1
    How about using [NSBlockOperation](https://developer.apple.com/library/Mac/DOCUMENTATION/Cocoa/Reference/NSBlockOperation_class/Reference/Reference.html)? – vikingosegundo Dec 10 '13 at 19:05
  • Thx guys for your answers, I edited my question. The method called takes a long time, and I have no access to it since it's from a library. The block is just a "completion" block called when the method finished the job – rmonjo Dec 10 '13 at 19:14
  • There is no general solution to your problem. There might be a solution specific to the library you're using, so stop making us play guessing games. Tell us what library you're using, and show us the real code. – rob mayoff Dec 10 '13 at 19:17
  • Since your question is so vague, I can only point you to Apple's guide to [Configuring Operations for Concurrent Execution](https://developer.apple.com/library/mac/documentation/general/conceptual/concurrencyprogrammingguide/OperationObjects/OperationObjects.html#//apple_ref/doc/uid/TP40008091-CH101-SW8). – titaniumdecoy Dec 10 '13 at 19:39
  • 1
    You are not telling us anything useful. But I'd guess that you have some asynchronous processing going on which is why `main` returns so quickly. Refactor your `callMethodOfALibrary...` method so it is synchronous. – rmaddy Dec 10 '13 at 19:54

1 Answers1

2

Little detail in your question, here is one possible outline which may help:

  • Create a semaphore (dispatch_semaphore_create)
  • Execute your library code asynchronously (dispatch_async)
  • Have the completion block for your library code signal the semaphore
  • Have your main method wait on the semaphore

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86