【问题标题】:Core Data 3 read and write data from indexCore Data 3 从索引读取和写入数据
【发布时间】:2016-11-14 05:20:37
【问题描述】:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do{
  cards = try context.fetch(Card.fetchRequest()).
  }
catch{
  print(error)
  }

此代码读取整个海量数据。如果我只想从 id 读取怎么办。就像这里的“id == %@”。这该怎么做?以及如何从 id 中编辑数据?

【问题讨论】:

    标签: swift core-data swift3


    【解决方案1】:
    let request = NSFetchRequest<MenuItems>(entityName: "MenuItems")
    let filters = NSPredicate(format: "id == %@", id)
    
    request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [filters])
    
    let results = (try? context.fetch(request)) ?? []
    

    如果您愿意,可以将更多NSPredicate 添加到filters

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多