Post

Replies

Boosts

Views

Activity

Reply to search array of HKQuantitySample (steps taken) with predicate for NSDates
Well a certain date is also a date range. Let’s say you want everything on the 18th of April 2020, then you check against >= 18th of April at 00:00 and < 19th of April at 00:00. You can also use smaller ranges, like hours or minutes. I don’t think it makes any sense to go even smaller than minutes. Your taken steps are saved with date and time. If you just check for "date == 18th of April” there will still be some time where you probably won’t have any data for.
Apr ’20
Reply to search array of HKQuantitySample (steps taken) with predicate for NSDates
You are checking against a certain date with time. There most likely is no set of data for that moment. You should use a date range. So something like "startDate > %@ AND startDate < %@".Also check out this function in HKQuery. That's basically what you wantopen class func predicateForSamples(withStart startDate: Date?, end endDate: Date?, options: HKQueryOptions = []) -> NSPredicate
Apr ’20
Reply to Json Batch insertion: Corrected sample from WWDC19 needed
I got it to work.There are some spelling mistakes on the presentation.The objects you pass in need to have the type [[String : Any]]So you basically have have key value pairs in a list. The strings must match the attribute names in your core data model.Also you need to cast the insertResult to NSBatchInsertResult. Not NSBatchInsertRequest.I used an example entity CDTest with the attributes "id" of type Int32 and "name" of type string. I made "id" the constraint.The following code worked for me then:class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.batchInsertExamples() } func batchInsertExamples() { var objectsToInsert: [[String : Any]] = [] for i in 0...5 { objectsToInsert.append(["id" : i, "name" : "My Name"]) } self.persistentContainer.viewContext.performAndWait { do { let insertRequest = NSBatchInsertRequest(entity: CDTest.entity(), objects: objectsToInsert) let insertResult = try self.persistentContainer.viewContext.execute(insertRequest) as! NSBatchInsertResult let success = insertResult.result as! Bool if !success { print("batch insert was not successful") } } catch { print(error.localizedDescription) } if self.persistentContainer.viewContext.hasChanges { do { try self.persistentContainer.viewContext.save() } catch { print("Error: \(error)\nCould not save Core Data context.") return } } } } lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "nsbatchinsert") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy container.viewContext.undoManager = nil container.viewContext.shouldDeleteInaccessibleFaults = true }) return container }() }
Apr ’20