当我的应用程序每天第一次启动时,我需要运行一些数据库处理。
我选择在第一个 View 中执行此操作,并显示带有事件指示器的警报 View 。我尝试使用 NSThread detachNewThreadSelector 来运行数据库处理。在函数结束时,我使用 dismissWithClickedButtonIndex
关闭警报 View 。
然后,如果日期允许,我会从 applicationWillEnterForeground
调用 View Controller 。
但是,我收到错误 尝试从主线程或 Web 线程以外的线程获取 Web 锁。现在在
。 dismissWithClickedButtonIndex
行崩溃
我认为发生这种情况是由于使用带有线程的警报 View 等对象时存在缺陷。
我需要它兼容 iOS 4。
有人能给我指出另一种方法吗?
最佳答案
您将无法在后台线程中直接创建和使用 UIAlertView。
以下是您可以做什么的大致想法:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
// create your progress indicator in your alert view
[alertView show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do your db update
dispatch_async(dispatch_get_main_queue(), ^{
// update your alert view here.
});
});
这个想法是在后台队列上进行处理,并将仅主线程对象的消息返回到主队列(例如UIAlertView
。)
关于iphone - Obj-C,在 View 中带有警报 View /进度指示的线程安全进程,可以从 applicationWillEnterForeground <= iOS4? 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9685255/