结果数量计算: resultType

  1. let fetchRequest =
  2. NSFetchRequest<NSNumber>(entityName: "Venue")
  3. fetchRequest.resultType = .countResultType
  4. fetchRequest.predicate = moderateVenuePredicate
  5. do {
  6. let countResult =
  7. try coreDataStack.managedContext.fetch(fetchRequest)
  8. let count = countResult.first?.intValue ?? 0
  9. let pluralized = count == 1 ? "place" : "places"
  10. secondPriceCategoryLabel.text =
  11. "\(count) bubble tea \(pluralized)"
  1. lazy var expensiveVenuePredicate: NSPredicate = {
  2. return NSPredicate(format: "%K == %@",
  3. #keyPath(Venue.priceInfo.priceCategory), "$$$")
  4. }()
  5. func populateExpensiveVenueCountLabel() {
  6. let fetchRequest: NSFetchRequest<Venue> = Venue.fetchRequest()
  7. fetchRequest.predicate = expensiveVenuePredicate
  8. do {
  9. let count =
  10. try coreDataStack.managedContext.count(for: fetchRequest)
  11. let pluralized = count == 1 ? "place" : "places"
  12. thirdPriceCategoryLabel.text =
  13. "\(count) bubble tea \(pluralized)"
  14. } catch let error as NSError {
  15. print("count not fetched \(error), \(error.userInfo)")
  16. }
  17. }

结果内字段数量总和 dictionaryResultType.

NSManagedObjectID 替代UID?

增加限制,否则获得的是所有的数据,存储到内存中

NSSortDescriptor

NSPredicate

NSAsynchronousFetchRequest

NSBatchUpdateRequest :所有邮件已读

NSBatchDeleteRequest:Delete all