【问题标题】:Swift Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never calledSwift 警告:应用程序委托收到对 -application:performFetchWithCompletionHandler 的调用:但从未调用过完成处理程序
【发布时间】: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

标签: ios swift


【解决方案1】:

警告告诉你添加这个方法:

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)
}

在这里阅读更多:

远程通知相关文章

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-03-18
    相关资源
    最近更新 更多