Rom*_*use 14 iphone objective-c ios
我有一个方法,我想在后面-viewDidLoad和后台线程中调用.有没有办法结合这两种方法:
[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
和
[self performSelectorInBackground:(SEL) withObject:(id)]?
Jos*_*ell 22
Grand Central Dispatch dispatch_after()将在指定队列上的指定时间后执行一个块.如果您创建后台队列,您将拥有所需的功能.
dispatch_queue_t myBackgroundQ = dispatch_queue_create("com.romanHouse.backgroundDelay", NULL);
// Could also get a global queue; in this case, don't release it below.
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
[self delayedMethodWithObject:someObject];
});
dispatch_release(myBackgroundQ);
Run Code Online (Sandbox Code Playgroud)
请尝试以下方法:
// Run in the background, on the default priority queue
dispatch_async( dispatch_get_global_queue(0, 0), ^{
[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
});
Run Code Online (Sandbox Code Playgroud)
代码未经测试
请注意,您的选择器/方法不能使用UIKit(因此不要更新UI)或访问UIKit属性(如框架),因此您的选择器可能需要将工作重新启动回主线程.例如
(id)SomeMethod:UsingParams: {
// Do some work but the results
// Run in the background, on the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// Do something UIKit related
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12631 次 |
| 最近记录: |