我在模拟器中调用此函数来模拟后台提取。
然后我在日志中收到此警告:
Swift警告:应用程序委托接收到对-application的调用:performFetchWithCompletionHandler:但是从未调用完成处理程序。
我已经看到其他Stack Iverflow的答案说我只需要添加completionhandler()
。我已经尝试了这个,它说我需要添加一个参数以及我丢失的地方。
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler:@escaping (UIBackgroundFetchResult) -> Void) {
let db = Firestore.firestore()
guard let uid = Auth.auth().currentUser?.uid else { return }
//check if user online
let docRef = db.collection("Users").document(uid)
docRef.getDocument { (document, error) in
if let document = document {
if document.exists {
guard let dictionary = document.data() else { return }
guard let onlineOfflineStatus = dictionary["Online Offline Status"] as? String else { return }
// if online create value to set offline an alert
if onlineOfflineStatus == "Online" {
print("user is Online and inactive, will set value to trigger notification asking if they would like to go offline")
db.collection("sendGoOffline").document(uid).setData(["OfflineAlert" : 1200], completion: { (error) in
if let error = error {
print("there was an error", error)
}
})
}
}
}
if let error = error {
print("failed to fetch user", error)
}
}
}
答案 0 :(得分:0)
警告告诉您添加此方法:
completionHandler(argument)
其中argument
是以下之一:
UIBackgroundFetchResult.noData
UIBackgroundFetchResult.newData
UIBackgroundFetchResult.failed
目的是告诉系统您已经完成。
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler:@escaping (UIBackgroundFetchResult) -> Void) {
// do backgound data fetch
// process it
// finished
completionHandler(UIBackgroundFetchResult.newData)
}
在此处了解更多信息
远程通知相关文章