【发布时间】:2018-06-17 23:52:31
【问题描述】:
我在模拟器下面调用这个函数来模拟后台抓取。
然后我在日志中收到此警告:
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)
}
}
}
【问题讨论】:
-
使用适当的参数值调用
completionHandler,如文档所示。 -
处理完数据后调用completion handler
-
任何人都可以给我代码示例我需要做什么即使阅读文档后我仍然不知道
-
您还需要帮助吗?
-
@ElTomato 我现在也有同样的问题。你能给样品电话吗?我不知道在
completionHandler()中输入什么参数,因为我没有UIBackgroundFetchResult。