Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios HKObjectQuery完成处理程序仅在监视处于活动状态时触发?_Ios_Swift_Apple Watch_Healthkit - Fatal编程技术网

Ios HKObjectQuery完成处理程序仅在监视处于活动状态时触发?

Ios HKObjectQuery完成处理程序仅在监视处于活动状态时触发?,ios,swift,apple-watch,healthkit,Ios,Swift,Apple Watch,Healthkit,使用下面的代码,我在Apple Watch上查询distance walking和running示例,我想返回给我(即,在用户移动时实时调用完成处理程序),但是,在测试中……当我移动时,不调用完成处理程序,只有当我抬起手腕并启动手表时才会调用它?如果可能的话,我想在后台收集这些数据。当训练开始时会触发查询,这一切都与唤醒或将激活功能无关 func startWalkingRunningQuery(from startDate: Date, updateHandler: @escaping ([H

使用下面的代码,我在Apple Watch上查询
distance walking和running
示例,我想返回给我(即,在用户移动时实时调用完成处理程序),但是,在测试中……当我移动时,不调用完成处理程序,只有当我抬起手腕并启动手表时才会调用它?如果可能的话,我想在后台收集这些数据。当训练开始时会触发查询,这一切都与
唤醒
将激活
功能无关

func startWalkingRunningQuery(from startDate: Date, updateHandler: @escaping ([HKQuantitySample]) -> Void) {
        let typeIdentifier = HKQuantityTypeIdentifier.distanceWalkingRunning
        startQuery(ofType: typeIdentifier, from: startDate) { _, samples, _, _, error in
            guard let quantitySamples = samples as? [HKQuantitySample] else {
                print("Distance walking running query failed with error: \(String(describing: error))")
                return
            }

            updateHandler(quantitySamples)

        }
    }

  //Generic helper function 
    private func startQuery(ofType type: HKQuantityTypeIdentifier, from startDate: Date, handler: @escaping
        (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void) {
        let datePredicate = HKQuery.predicateForSamples(withStart: startDate, end: nil, options: .strictStartDate)
        let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
        let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])

        let quantityType = HKObjectType.quantityType(forIdentifier: type)!

        let query = HKAnchoredObjectQuery(type: quantityType, predicate: queryPredicate, anchor: nil,
                                          limit: HKObjectQueryNoLimit, resultsHandler: handler)
        query.updateHandler = handler
        healthStore.execute(query)

        activeDataQueries.append(query)
    }

你的应用程序是否开始训练课程?是的,我正在开始训练课程你的应用程序是否开始训练课程?是的,我正在开始训练课程