【问题标题】:BackgroundFetch is not woking, ‘updateTime’ function in not calling in 10 second后台获取不工作,'dateTime' 功能在 10 秒内不调用
【发布时间】:2020-06-17 09:35:30
【问题描述】:

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

       UIApplication.shared.setMinimumBackgroundFetchInterval(10)

        return true
    }

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

      if let viewController = window?.rootViewController as? ViewController {

        viewController.updateTime()
        completionHandler(.newData)
      } else {

        completionHandler(.noData)
        }


}

视图控制器:

func updateTime() {

        currentTime = Date()
        let formatter = DateFormatter()
        formatter.timeStyle = .short

        if let currentTime = currentTime {

            var arr : NSMutableArray = NSMutableArray()

            if UserDefaults.standard.value(forKey: "time") != nil {

                arr = NSMutableArray(array: UserDefaults.standard.value(forKey: "time") as! NSArray)
            }

            arr.add(currentTime)

            UserDefaults.standard.set(arr, forKey: "time")
            UserDefaults.standard.synchronize()

            Info_LB.text = formatter.string(from: currentTime)
        }
    }

【问题讨论】:

标签: ios swift


【解决方案1】:

wwdc2019

https://developer.apple.com/videos/play/wwdc2019/707/

只有在系统决定时才会执行任务

您也可以查看此论坛讨论链接:

https://forums.developer.apple.com/thread/124320

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2017-03-03
    • 2023-03-29
    • 2017-10-15
    • 1970-01-01
    • 2020-03-11
    • 2011-07-09
    相关资源
    最近更新 更多