Reputation: 45
I have a View Controller where the GUI logic lies
and I have a thread which is started from this view controller and there I am doing some processing
I have two views which i want to flip after some time from my thread
As I know we can do the flipping from view controllers only
So please suggest me a way to call method of view controller from my custom thread class.
Thanks in advance
Upvotes: 0
Views: 447
Reputation: 4174
In your thread class, add a pointer to your view controller:
@property (nonatomic, assign) YourViewController *viewController;
When your view controller creates the thread, set the thread's viewController
property to your view controller:
yourThreadClass.viewController = self;
In your View Controller class, create a flipViews
method:
-(void)flipViews
{
// ...
}
In your thread class, whenever it needs to flip the views, just call:
[viewController performSelectorOnMainThread:@selector:(flipViews) withObject:nil waitUntilDone:YES];
Upvotes: 0
Reputation: 10175
So first options that comes trough my mind are:
If you have access to the view controller from your thread class then you could do:
dispatch_async(dispatch_get_main_queue(), ^{
[yourVc callMethodToFlip];
});
If you don't have access you can use notifications:
YourVC.m
-(void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(methodForFlip) name:A_NOTIFICATION_NAME object:nil];
}
InYourTreadClass.m
-(void)someMethodThatExecutesAsync {
//dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:A_NOTIFICATION_NAME
object:nil]; //or yoou can send an object
});
}
The other method would be, create a method for your custom thread class that takes a block parameter, whenever you create a thread from your vc send the block parameter which will be called from your thread also using dispatch_async
Upvotes: 1
Reputation: 130193
You should use Grand Central Dispatch (GCD) to perform your asynchronous operations. That way you can perform intensive operations off of the main thread, and then come back to the main thread to perform your animations with this simple syntax:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{
//Calculations
dispatch_async(dispatch_get_main_queue(), ^{
//Do animations
});
});
Upvotes: 1