结果数量计算: resultType
let fetchRequest =
NSFetchRequest<NSNumber>(entityName: "Venue")
fetchRequest.resultType = .countResultType
fetchRequest.predicate = moderateVenuePredicate
do {
let countResult =
try coreDataStack.managedContext.fetch(fetchRequest)
let count = countResult.first?.intValue ?? 0
let pluralized = count == 1 ? "place" : "places"
secondPriceCategoryLabel.text =
"\(count) bubble tea \(pluralized)"
lazy var expensiveVenuePredicate: NSPredicate = {
return NSPredicate(format: "%K == %@",
#keyPath(Venue.priceInfo.priceCategory), "$$$")
}()
func populateExpensiveVenueCountLabel() {
let fetchRequest: NSFetchRequest<Venue> = Venue.fetchRequest()
fetchRequest.predicate = expensiveVenuePredicate
do {
let count =
try coreDataStack.managedContext.count(for: fetchRequest)
let pluralized = count == 1 ? "place" : "places"
thirdPriceCategoryLabel.text =
"\(count) bubble tea \(pluralized)"
} catch let error as NSError {
print("count not fetched \(error), \(error.userInfo)")
}
}
结果内字段数量总和 dictionaryResultType.
NSManagedObjectID 替代UID?
增加限制,否则获得的是所有的数据,存储到内存中
NSSortDescriptor
NSPredicate
NSAsynchronousFetchRequest
NSBatchUpdateRequest :所有邮件已读
NSBatchDeleteRequest:Delete all