开发者社区> 问答> 正文

移动推送 iOS SDK: Main Thread Checker: UI API called on a background thread


Xcode 升级到 Xcode 9后,集成时若提示下述错误:

  1. [backcolor=transparent]Main[backcolor=transparent] [backcolor=transparent]Thread[backcolor=transparent] [backcolor=transparent]Checker[backcolor=transparent]:[backcolor=transparent] UI API called on a background thread

请检查工程中,是否在后台线程(非主线程)调用 AppKit、UIKit相关的API,比如iOS 10+ 请求通知权限时,[application registerForRemoteNotifications];在回调非主线程中执行,则Xcode 9会报上述错误。
  1. [backcolor=transparent][[backcolor=transparent]_notificationCenter requestAuthorizationWithOptions[backcolor=transparent]:[backcolor=transparent]UNAuthorizationOptionAlert[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionBadge[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionSound[backcolor=transparent] completionHandler[backcolor=transparent]:^([backcolor=transparent]BOOL granted[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSError[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]_Nullable[backcolor=transparent] error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]granted[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]        [backcolor=transparent]// granted
  4. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User authored notification."[backcolor=transparent]);
  5. [backcolor=transparent]        [backcolor=transparent]// 向APNs注册,获取deviceToken
  6. [backcolor=transparent]        [backcolor=transparent][[backcolor=transparent]application registerForRemoteNotifications[backcolor=transparent]];
  7. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]        [backcolor=transparent]// not granted
  9. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User denied notification."[backcolor=transparent]);
  10. [backcolor=transparent]    [backcolor=transparent]}
  11. [backcolor=transparent]}];

应修改为:
  1. [backcolor=transparent][[backcolor=transparent]_notificationCenter requestAuthorizationWithOptions[backcolor=transparent]:[backcolor=transparent]UNAuthorizationOptionAlert[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionBadge[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNAuthorizationOptionSound[backcolor=transparent] completionHandler[backcolor=transparent]:^([backcolor=transparent]BOOL granted[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]NSError[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]_Nullable[backcolor=transparent] error[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]granted[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]        [backcolor=transparent]// granted
  4. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User authored notification."[backcolor=transparent]);
  5. [backcolor=transparent]        [backcolor=transparent]// 向APNs注册,获取deviceToken
  6. [backcolor=transparent]        dispatch_async[backcolor=transparent]([backcolor=transparent]dispatch_get_main_queue[backcolor=transparent](),[backcolor=transparent] [backcolor=transparent]^{
  7. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]application registerForRemoteNotifications[backcolor=transparent]];
  8. [backcolor=transparent]        [backcolor=transparent]};
  9. [backcolor=transparent]    [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]else[backcolor=transparent] [backcolor=transparent]{
  10. [backcolor=transparent]        [backcolor=transparent]// not granted
  11. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User denied notification."[backcolor=transparent]);
  12. [backcolor=transparent]    [backcolor=transparent]}
  13. [backcolor=transparent]}];

展开
收起
猫饭先生 2017-10-24 10:09:49 1784 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载