代码之家  ›  专栏  ›  技术社区  ›  user7684436

快速任务没有调用类

  •  2
  • user7684436  · 技术社区  · 7 年前

    didReceiveMessage 通过 WatchConnectivity

    后台任务需要执行一些操作,例如打开realm数据库、查询结果和访问documents目录,然后将响应返回到courses字典。当手表输出其获得的航向数据时,逻辑似乎起作用了。问题是我认为后台任务实际上并没有调用该方法 getWatchCourses()

    DispatchQueue.global().async {
    
        var foundCourses = [[String : Any]]()
        let application = UIApplication.shared
        backgroundTask = application.beginBackgroundTask(withName: "app.test.getCourses") {
            let watchModel = WatchCourseModel()
            let courses = watchModel.getWatchCourses()
            print("Courses: \(courses)")
            foundCourses.append(contentsOf: courses)
        }
        replyHandler(["response": foundCourses])
        application.endBackgroundTask(backgroundTask)
        backgroundTask = UIBackgroundTaskInvalid
    }
    

    如果 getWatchCourses()

    还有一点值得指出的是,网上没有这方面的记录, 他们总是指把简单的短信回复回手表,

    谢谢

    3 回复  |  直到 7 年前
        1
  •  4
  •   Prashant Tukadiya    7 年前

    您正在执行用于清理的尾部闭合的操作。更重要的是,一旦后台任务开始,你就要结束它,这样你的结束就永远不会调用。

    下面是后台任务如何工作的示例

     var backgroundTaskIdentifier:UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
     var updateTimer: Timer?
    

    当你现在开始运行计时器的时候

      updateTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self,
                                         selector: #selector(calculateNextNumber), userInfo: nil, repeats: true)
      // register background task
      beginBackgroundTask()
    

    当你结束计时器时,结束后台任务;应该总是不失败地结束后台任务

    func beginBackgroundTask () {
        backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in
          self?.endBackgroundTask() // It will call to cleanup 
        })
        assert(backgroundTaskIdentifier != UIBackgroundTaskInvalid)
      }
    
      func endBackgroundTask () {
        UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
        backgroundTaskIdentifier = UIBackgroundTaskInvalid
      }
    

    • 注册后台任务
    • 当您完成呼叫结束后台任务时

    编辑

    确保已启用后台功能窗体项目设置

    试试看

        beginBackgroundTask()
    
        let watchModel = WatchCourseModel()
        let courses = watchModel.getWatchCourses()
        print("Courses: \(courses)")
        foundCourses.append(contentsOf: courses)
        endBackgroundTask()
    
        2
  •  2
  •   Paulw11    7 年前

    如果你检查 documentation beginBackgroundTask(withName:expirationHandler:)

    通过尾部闭包指定的代码是 expirationHandler -如果你不打电话,它就是被调用的代码 endBackgroundTask

    从函数返回时,将不会调用过期处理程序。

    var foundCourses = [[String : Any]]()
    let application = UIApplication.shared
    backgroundTask = application.beginBackgroundTask(withName: "app.test.getCourses") {
    DispatchQueue.global().async {
        let watchModel = WatchCourseModel()
        let courses = watchModel.getWatchCourses()
        print("Courses: \(courses)")
        foundCourses.append(contentsOf: courses)
        replyHandler(["response": foundCourses])
        application.endBackgroundTask(backgroundTask)
        backgroundTask = UIBackgroundTaskInvalid
    }
    

    这将启动一个后台任务,然后异步执行该工作,并将结果返回给监视程序并结束后台任务。

        3
  •  1
  •   Paulw11    7 年前

    您可以使用QOS类型as.background创建背景任务

    DispatchQueue.global(qos: .background).async {
            //background code
            var foundCourses = [[String : Any]]()
            let application = UIApplication.shared
            backgroundTask = application.beginBackgroundTask(withName: "app.test.getCourses") {
                let watchModel = WatchCourseModel()
                let courses = watchModel.getWatchCourses()
                print("Courses: \(courses)")
                foundCourses.append(contentsOf: courses)
            }
            replyHandler(["response": foundCourses])
            DispatchQueue.main.async {
                //your main thread
            }
        }