簡體   English   中英

調用“UNUserNotificationCenter”的完成處理程序時崩潰

[英]Crash when calling completion handler for `UNUserNotificationCenter`

我正在開發一個應用程序,當收到靜默推送通知時; 將進行兩次網絡調用以獲取一些數據,然后使用該數據創建本地推送通知。

這主要適用於前景和背景; 除了一些我很難診斷的偶爾崩潰。

這是我在靜默推送通知到達時調用的委托方法的完整實現:

 func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
       
            print("Notification received!")
            UNUserNotificationCenter.current().delegate = self
            let id = updateNotificationID()
            let payload = userInfo
            
            guard let ship: String = payload["ship"] as? String else {
                return
            }
            
            guard let node: String = payload["node"] as? String else {
                return
            }

            guard let graph: String = payload["graph"] as? String else {
                return
            }

            var groupName: String = ""
            
            networkStore.scryForGroupInfo(ship: ship, graph: graph) { groupTitle in
                
                if groupTitle == "" {
                    //one on one
                } else {
                    //groupchat
                    groupName = groupTitle
                }
                
                networkStore.scryOnNotificationReceipt(ship: ship, node: node, graph: graph) { [self] messageString, author  in
                    let content = UNMutableNotificationContent()

                    if groupName == "" {
                        //group chat
                        content.title = "New Message from \(author)"
                        let chatShipDict:[String: String] = ["ChatShip": author]
                        content.userInfo = chatShipDict
                    } else {
                        content.title = "\(author) posted in \(groupName)"
                        let chatShipDict:[String: String] = ["ChatShip": groupName]
                        content.userInfo = chatShipDict
                    }
                    
                    //Don't show a notification for a chat that we're currently in
                    if airlockStore.selectedChannel != nil {
                        let author = "~"+author
                        if airlockStore.selectedChannel.channelShip == author || airlockStore.selectedChannel.channelName == groupName {
                            completionHandler(.noData)
                            return
                        }
                    } else {
                        //One on one chat
                        if groupName == "" {
                            let author = "~"+author
                            for channel in airlockStore.unPinnedChannelDataObjects {
                                if channel.channelShip == author  {
                                    notificationChannel = channel
                                }
                            }
                        } else {
                            //Group chat
                            for channel in airlockStore.unPinnedChannelDataObjects {
                                if channel.channelName == groupName {
                                    notificationChannel = channel
                                }
                            }
                        }
                    }
                    
                    content.body = messageString

                    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
                    let request = UNNotificationRequest(identifier: "notification.id.\(id)", content: content, trigger: trigger)
                    
                    UNUserNotificationCenter.current().add(request) { Error in
                        print("Showing Notification")
                        completionHandler(.newData)
                    }
                }
            }
    }

崩潰似乎發生在completionHandler(.newData)行。

這是我的堆棧跟蹤:

Thread 10 name:
Thread 10 Crashed:
0   libdispatch.dylib               0x00000001a0b147b8 dispatch_group_leave.cold.1 + 36 (semaphore.c:303)
1   libdispatch.dylib               0x00000001a0ae0668 dispatch_group_leave + 140 (semaphore.c:0)
2   Pocket                          0x000000010464333c partial apply for closure #1 in closure #1 in closure #1 in AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:) + 136 (AppDelegate.swift:137)
3   Pocket                          0x00000001046419ac thunk for @escaping @callee_guaranteed (@guaranteed Error?) -> () + 44 (<compiler-generated>:0)
4   libdispatch.dylib               0x00000001a0adda84 _dispatch_call_block_and_release + 32 (init.c:1466)
5   libdispatch.dylib               0x00000001a0adf81c _dispatch_client_callout + 20 (object.m:559)
6   libdispatch.dylib               0x00000001a0ae7004 _dispatch_lane_serial_drain + 620 (inline_internal.h:2557)
7   libdispatch.dylib               0x00000001a0ae7c34 _dispatch_lane_invoke + 456 (queue.c:3862)
8   libdispatch.dylib               0x00000001a0af24bc _dispatch_workloop_worker_thread + 764 (queue.c:6589)
9   libsystem_pthread.dylib         0x00000001ecc7e7a4 _pthread_wqthread + 276 (pthread.c:2437)
10  libsystem_pthread.dylib         0x00000001ecc8574c start_wqthread + 8
                        

您說您正在撥打兩個不同的網絡電話。 您確定有時不會兩次調用完成處理程序嗎? 確保它只被調用一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM