I have an object that has a method that returns its results over a delegate method, a couple of seconds later. I understand that if I do this on another thread using GCD I can improve my UI performance, but what about the delegate method that is implemented on the object that is running on the main thread?
So this all happens in the main thread:
On the header file:
@interface MyViewController : UIViewController <ThisRespondsToSomeDelegate>
On the implementation file of the view controller:
SomeObject _myDealer; // INSTANCE VARIABLE OF THE OBJECT THAT IMPLEMENTS THE ThisRespondsToSomeDelegate TYPE
Later on the same implementation file:
[_myDealer getSomeData];
And later, the delegate method:
-(void)myDealerReturnedSomeData:data anotherArg:(id)somemoredata {...};
From what I understand, I create a *background_queue* object with the *dispatch_queue_create* method and than use:
dispatch_async(backgroundQueue, ^(void) { [_myDealer getSomeData]; });
But how do I return to the delegate method on the main thread? How does myDealerReturnedSomeData:data get called?
Thanks!
Happy coding!
To call a method on a main thread after a background GCD task completes, simply dispatch it in a block to a main queue after your background task completes its execution: