iphone - Obj-C,在 View 中带有警报 View /进度指示的线程安全进程,可以从 applicationWillEnterForeground <= iOS4? 调用

标签 iphone objective-c ios cocoa-touch

当我的应用程序每天第一次启动时,我需要运行一些数据库处理。

我选择在第一个 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/

相关文章:

php - 确定 iOS 设备类型

iphone - 如何在Objective C代码中使用Class对象

objective-c - Twitter API 的签名是如何真正起作用的?

javascript - iOS Safari 顶部和底部栏问题

ios - 仅当应用程序打开并可见时才通过远程通知提醒用户?

ios - Xcode8 提交错误-如何在 iOS10 上找出究竟需要特定权限的内容

ios - 无法从应用程序组 ios 中的共享容器中读取

iphone - 如何在 iPhone 中创建网格 TableView

ios - 关于带有圆角的 UIImageView

ios - 从 TableView objective-c 中删除元素