批处理查询数据
let batchUpdate = NSBatchUpdateRequest(entityName: "Venue")
batchUpdate.propertiesToUpdate = ["favorite" : NSNumber(bool: true)]
batchUpdate.affectedStores = coreDataStack.context.persistentStoreCoordinator!.persistentStores
batchUpdate.resultType = .UpdatedObjectsCountResultType
do {
let batchResult =
try coreDataStack.context.executeRequest(batchUpdate) as! NSBatchUpdateResult
print("Records updated \(batchResult.result!)")
} catch let error as NSError {
print("Could not update \(error), \(error.userInfo)")
}
多线程查询数据
fetchRequest = NSFetchRequest(entityName: "Venue")
asyncFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest, completionBlock: {
[unowned self] (result: NSAsynchronousFetchResult!) -> Void in
self.venues = result.finalResult as! [Venue]
self.tableView.reloadData()
})
do {
try coreDataStack.context.executeRequest(asyncFetchRequest)
} catch let error as NSError {
print("could not fetch \(error), \(error.userInfo)")
}
用NSAsynchronousFetchRequest包裹NSFetchRequest
执行更新的是使用executeRequest