iOS Today 扩展崩溃

标签 ios swift crash today-extension

我们最近在我们的应用程序中添加了 Today Extension。

它是一个带有简单 tableView 的扩展,用于从我们的服务器加载数据,用 Swift 编写。

但在扩展对我们的用户在线后,我们从 Crashlytics 收到了大量崩溃数据,Apple 也报告了同样的崩溃问题。

但是,我们无法重现崩溃,甚至无法找到崩溃的原因,因为崩溃发生在系统调用的widgetPerformUpdateWithCompletionHandler方法上。

以下是崩溃日志的详细信息,部分信息被替换。

Crashed: com.apple.main-thread
0  MyAppWidget                    0x100091d8c MyAppWidgetViewController.(widgetPerformUpdateWithCompletionHandler((NCUpdateResult) -> ()) -> ()).(closure #1) (MyAppWidgetViewController.swift)
1  MyAppKit                       0x100603ab4 partial apply for thunk (PostService.swift)
2  MyAppKit                       0x100626080 ServiceRequestPerfomer.(performRequest(A, complete : ([B]?, NSError?) -> ()?) -> ()).(closure #1) (ServiceRequestPerformer.swift:35)
3  MyAppKit                       0x1006258d4 partial apply for ServiceRequestPerfomer.(performRequest(A, complete : ([B]?, NSError?) -> ()?) -> ()).(closure #1) (ServiceRequestPerformer.swift)
4  MyAppKit                       0x100626b34 ServiceRequestPerfomer.((request in _3C50B415180DDC893FFCB75CD7EE7019)(A, complete : (response : Response?, error : NSError?) -> ()?) -> ()).(closure #1) (ServiceRequestPerformer.swift:144)
5  Moya                           0x1004b7468 MoyaProvider.(requestNormal(A, queue : OS_dispatch_queue?, progress : (progress : ProgressResponse) -> ()?, completion : (result : Result<Response, Error>) -> ()) -> Cancellable).(closure #1).(closure #1) (Moya.swift:229)
6  Moya                           0x1004b8968 MoyaProvider.((sendAlamofireRequest in _1A5616FEE5C423A992964CB19AABD52B)(Request, target : A, queue : OS_dispatch_queue?, progress : (progress : ProgressResponse) -> ()?, completion : (result : Result<Response, Error>) -> ()) -> CancellableToken).(closure #3) (Moya.swift)
7  Moya                           0x1004b57b8 partial apply for MoyaProvider.((sendAlamofireRequest in _1A5616FEE5C423A992964CB19AABD52B)(Request, target : A, queue : OS_dispatch_queue?, progress : (progress : ProgressResponse) -> ()?, completion : (result : Result<Response, Error>) -> ()) -> CancellableToken).(closure #3) (Moya.swift)
8  Alamofire                      0x1002cff64 Request.(response(queue : OS_dispatch_queue?, completionHandler : (NSURLRequest?, NSHTTPURLResponse?, NSData?, NSError?) -> ()) -> Self).(closure #1).(closure #1) (ResponseSerialization.swift)
9  libdispatch.dylib              0x1819e14bc _dispatch_call_block_and_release + 24
10 libdispatch.dylib              0x1819e147c _dispatch_client_callout + 16
11 libdispatch.dylib              0x1819e6b84 _dispatch_main_queue_callback_4CF + 1844
12 CoreFoundation                 0x181f4cd50 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
13 CoreFoundation                 0x181f4abb8 __CFRunLoopRun + 1628
14 CoreFoundation                 0x181e74c50 CFRunLoopRunSpecific + 384
15 GraphicsServices               0x18375c088 GSEventRunModal + 180
16 UIKit                          0x18715e088 UIApplicationMain + 204
17 libxpc.dylib                   0x181c38ce0 _xpc_objc_main + 784
18 libxpc.dylib                   0x181c3a9dc xpc_main + 200
19 Foundation                     0x182a57d60 service_connection_handler + 170
20 PlugInKit                      0x18929ac48 -[PKService run] + 544
21 PlugInKit                      0x18929a8dc +[PKService main] + 56
22 PlugInKit                      0x18929ac6c +[PKService _defaultRun:arguments:] + 20
23 libextension.dylib             0x18286a058 NSExtensionMain + 64
24 libdispatch.dylib              0x181a128b8 (Missing)

以及widgetPerformUpdateWithCompletionHandler的代码

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    // Perform any setup necessary in order to update the view.
    // If an error is encountered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    let options: [String: AnyObject] = [DCKPostPopularAttributeName : true.stringValue,
                                        DCKPaginationLimitAttributeName : 5]
    postService.posts(options: options) { (posts, error) in
        let historyPosts = self.userDefaults.arrayForKey(MyAppWidgetViewController.WidgetPostReadIdentifier) as? [UInt]
        if let historyPosts = historyPosts {
            self.posts = posts!.filter({ (Post) -> Bool in
                return !historyPosts.contains(Post.id)
            })
        } else {
            self.posts = posts!
        }

        self.tableView.reloadData()
        self.tableView.hidden = false;
        self.loadingLabel?.hidden = true
        self.activityIndicator?.stopAnimating()

        let contentSize = self.tableView.contentSize
        self.preferredContentSize = CGSizeMake(CGRectGetWidth(self.view.frame), contentSize.height);

        self.loadMorePostsIfNeeded()
    }
    completionHandler(NCUpdateResult.NewData)
}

奇怪的是:

  1. 我们在正常使用 Today Extension 时没有遇到崩溃问题(它应该显示 Unable to load 如果 today extension 崩溃)

  2. 我们没有收到任何用户报告今天扩展程序无法正常工作的问题。

  3. 我们在我们的设备上发现了相同的崩溃日志,但今天的扩展正常工作,如 1. 所述。还有,记录崩溃日志的时间,我没有用我的手机!

所以我们猜测这是一次“假”崩溃,记录了崩溃,但扩展程序并没有真正崩溃。

有人遇到同样的问题吗? 谢谢!

最佳答案

回答我自己的问题,

@matt 是对的,现在我调用 completionHandler仅当数据获取任务完成时,而不是总是调用 completionHandlerwidgetPerformUpdateWithCompletionHandler 的最后方法。

我没有再收到崩溃,问题解决了!

关于iOS Today 扩展崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39114127/

相关文章:

ios - 什么是 objc_selector 实现?

ios - 如何在 iPhone 应用程序中集成 AdMob

ios - 如何将单独函数中生成的随机数同步到另一个@IBAction func

swift - NSPersistentDocument、Swift、macOS 和 Storyboard — 如何获取 ManagedObjectContext?

ios - 尝试从 super View 中删除对象时 Objective-c 程序崩溃

c++ - ofstream::open 创建文件,但随后崩溃(locale::getloc() 中的错误指针?)

ios - iOS 中的多选

ios - 在使用嵌套框架进行 IPA 验证期间协​​同设计失败

ios - 如何在单行 Collection View 中显示选定的照片

java - 在 client.execute(httpGet) 的 doInBackground() 行上发生崩溃。为什么?