1

事情是这样的,我想将今天的步骤保存到完成处理程序中的核心数据中。但是当我加载这个值时,编译器显示它为零。任何解决方案?

 func fetchDataOfQuantityType(startDate: NSDate, endDate: NSDate, quantityType: HKQuantityType, completion:((NSArray, NSError!) -> Void)!)  {

    // initial a predicate with startDate and endDate
    let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: HKQueryOptions.StrictStartDate)

    // initialize a HKStatisticsQuery
    let query: HKStatisticsQuery = HKStatisticsQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: HKStatisticsOptions.CumulativeSum, completionHandler: { (query, results, error) in

        if error != nil { // if there is an error print it
            println("there is a \(error) occur")
            return
        }
        // Mark: - Saving the data in to Core data

        var todaySteps = results.sumQuantity().doubleValueForUnit(HKUnit.countUnit())
        println("The totalstep for today is \(todaySteps)")
            var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
            var context: NSManagedObjectContext = appDel.managedObjectContext!
            var newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: context) as NSManagedObject
            newUser.setValue(todaySteps, forKey: "todaysteps")
            })
    self.healthstore.executeQuery(query)
}
4

1 回答 1

0

这看起来像你没有保存上下文。尝试

var error: NSError?
context.save(&error)

在你设置了值之后。

于 2014-11-22T08:42:13.267 回答