//viewcontroller.m
-(void)viewdidLoad
{
self.theOneViewController= [[TheOneViewController alloc]init];
[contentsView addSubview:self.theOneViewController.view];
}
//theOneViewController
- (void)viewDidLoad
{ .
.
.
//UI WORK
.
.
//LONG WORK
[self performSelectorOnMainThread:@selector(initAppList) withObject:nil waitUntilDone:NO];
}
this code, view display UI WORK before LONG WORK is end.So I can have a thread effect.
//viewcontroller.m
-(void) buttonPressed:(id)sender -> event method
{
self.theOneViewController= [[TheOneViewController alloc]init];
[contentsView addSubview:self.theOneViewController.view];
}
//theOneViewController
- (void)viewDidLoad
{ .
.
.
//UI WORK
.
.
//LONG WORK
[self performSelectorOnMainThread:@selector(initAppList) withObject:nil waitUntilDone:NO];
}
In this code, view display UI WORK after LONG WORK is end.
So I can’t have thread effect. why?
And I use (performSelectorInBackground:withObject:) instead of (performSelectorOnMainThread withObject:waitUntilDone:) . but this is slower than not using thread.
I want to have thread effect in event method call.
Is there a good way? help me please!
Your question isn’t particularly clear but I’ll have a go at answering.
You are calling
performSelectorOnMainThread:withObject:waitUntilDone:from either yourviewDidLoadorbuttonPressedmethods. The problem with this is that all UI handling (viewDidLoad, UI events etc) is done on the main thread which means that callingperformSelectorOnMainThreadwill just queue the method on the main threads runloop. This probably means that the method will run immediately after the calling method completes.Ultimately, you aren’t really gaining much by doing this over just calling the method directly as the main thread will take about the same amount of time to do either way.
Calling
performSelectorOnBackgroundThread:withObject:will run theinitAppListmethod on a separate thread, allowing the main thread to continue dealing with the UI. However, as you note, that will probably be a little bit slower overall as there is the overhead of creating the background thread.