簡體   English   中英

Firebase 抓取導致部分用戶崩潰

[英]Firebase fetching causes crash for some users

當我嘗試從 Firebase 獲取用戶數據時,某些用戶發生崩潰,我自己無法重現此崩潰,但我確實有以下崩潰日志:

0  Ski Tracker                    0x77bf0 closure #1 in HistoryPresenter.downloadHistory(completionHandler:) + 4340857840 (HistoryPresenter.swift:4340857840)
1  Ski Tracker                    0x86c8 closure #1 in FetchFromDatabase.fetchUserHistoryFromDatabase(uid:completionHandler:) + 4340401864 (<compiler-generated>:4340401864)
2  Ski Tracker                    0x8604 thunk for @escaping @callee_guaranteed (@guaranteed FIRDataSnapshot) -> () + 4340401668 (<compiler-generated>:4340401668)
3  FirebaseDatabase               0x1df28 __92-[FIRDatabaseQuery observeSingleEventOfType:andPreviousSiblingKeyWithBlock:withCancelBlock:]_block_invoke + 120
4  FirebaseDatabase               0xbf94 __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.11 + 80
5  libdispatch.dylib              0x24b4 _dispatch_call_block_and_release + 32
6  libdispatch.dylib              0x3fdc _dispatch_client_callout + 20
7  libdispatch.dylib              0x127f4 _dispatch_main_queue_drain + 928
8  libdispatch.dylib              0x12444 _dispatch_main_queue_callback_4CF + 44
9  CoreFoundation                 0x9a6f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
10 CoreFoundation                 0x7c058 __CFRunLoopRun + 2036
11 CoreFoundation                 0x80ed4 CFRunLoopRunSpecific + 612
12 GraphicsServices               0x1368 GSEventRunModal + 164
13 UIKitCore                      0x3a23d0 -[UIApplication _run] + 888
14 UIKitCore                      0x3a2034 UIApplicationMain + 340
15 libswiftUIKit.dylib            0x35308 UIApplicationMain(_:_:_:_:) + 104
16 Ski Tracker                    0x7160 main + 4340396384 (FriendView.swift:4340396384)
17 ???                            0x1f6938960 (Missing)

如果我正確理解崩潰日志,導致崩潰的代碼在 fetchUserHistoryFromDatabase function 中:

func fetchUserHistoryFromDatabase(uid : String, completionHandler: @escaping([String : Any]?) -> Void ) {

        ref?.child("users").child(uid).child("runData").observeSingleEvent(of: .value, with: { snapshot in
            
            guard let result = snapshot.value as? [String:Any] else {
                print("Error no rundata")
                completionHandler(nil)
                return
            }

            completionHandler(result)
            
        })
    }

這個 function 從處理潛在的 nil 值的 downloadHistory 調用:

private func downloadHistory(completionHandler: @escaping () -> Void) {
        if let id = Auth.auth().currentUser?.uid {
            FetchFromDatabase().fetchUserHistoryFromDatabase(uid : id, completionHandler: { [weak self] dict in
                
                if dict != nil {
                    for run in dict! {
                        self?.determineTimeStamp(run : run)
                    }
                    if !(self!.tempDict.isEmpty) {
                        let sortedDict = self?.tempDict.keys.sorted(by: { $0 > $1 } )
                        self?.convertDictToArray(sortedDict: sortedDict!)
                    }
                }
                completionHandler()
            }
        )}
    }

非常感謝這里的任何幫助。

從您的代碼中刪除強制展開。 每一個! 是崩潰的邀請。

private func downloadHistory(completionHandler: @escaping () -> Void) {
    if let id = Auth.auth().currentUser?.uid {
        FetchFromDatabase().fetchUserHistoryFromDatabase(uid : id, completionHandler: { [weak self] dict in
            guard let self = self else {
                completion()
                return
            }
            if let dict = dict {
                for run in dict {
                    self.determineTimeStamp(run : run)
                }
                if !self.tempDict.isEmpty {
                    let sortedDict = self.tempDict.keys.sorted(by: { $0 > $1 } )
                    self.convertDictToArray(sortedDict: sortedDict)
                }
            }
            completionHandler()
        }
    )}
}

我注意到一個self! 那里很危險,因為用戶可以離開 function 的調用上下文,並且由於閉包有一個弱自我的捕獲列表,它應該返回 nil 但你強迫它

嘗試這個

   private func downloadHistory(completionHandler: @escaping () -> Void) {
                if let id = Auth.auth().currentUser?.uid {
                    FetchFromDatabase().fetchUserHistoryFromDatabase(uid : id, completionHandler: { [weak self] dict in
                        guard let self = self else { completionHandler() 
return }
                        if let safeDict = dict {
                            for run in dict {
                                self.determineTimeStamp(run : run)
                            }
                            if (self.tempDict.isEmpty) {
                                let sortedDict = self.tempDict.keys.sorted(by: { $0 > $1 } )
                                self.convertDictToArray(sortedDict: sortedDict)
                            }
                        }
                        completionHandler()
                    }
                )}
            }

暫無
暫無

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

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