Domanda

We're implementing a sync system where some operations are sent to a SAP Mobile Platform server asynchronously, and when the operations are completed, we receive a delegate call from a proprietary SDK.

This delegate method is run on a random thread, not using GCD.

We would like to do send the operations - then wait until the delegate method is called which means the operations are complete - then resume the work when this is finished (or a time-out has occured). I know this may seem synchronous, but we cannot allow the user to modify data while the operations are not finished yet to ensure data integrity.

The Sync ViewController is doing some heavy syncing work and is using GCD, and updates a progress bar and some text in the UI thread.

Now, when the delegate method is called, we want to call another method on the Sync ViewController, but this call is apparently done in the wrong thread.

My question is how to execute these methods in the same thread that GCD was executing them before the delegate was called.

Things we tried:

  1. Just calling the methods. Then these methods are called in the wrong thread, namely the thread where the delegate method is in.
  2. Posting a notification on a chosen thread, we don't know the exact thread where the Sync ViewController was working in.

Any ideas?

È stato utile?

Soluzione

If you are already using GCD, then you must know the dispatch queue this work is being done on, so isn't it simply a case of scheduling a block, either synchronously or asynchronously, on that queue?

- (void)someDelegateMethod:(id)someValue {
    dispatch_async(self.myDispatchQueue, ^{
        [self doInterestingThingWith:someValue];
    });
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top