Nikolay Borisov Nikolay Borisov - 24 days ago 12
Swift Question

Core Data 3 read and write data from index

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do{
cards = try context.fetch(Card.fetchRequest()).
}
catch{
print(error)
}


This code reads whole massive data. What if i wanna read only from id. Like here "id == %@". How to do this? And how edit data from id?

Answer
let request = NSFetchRequest<MenuItems>(entityName: "MenuItems")
let filters = NSPredicate(format: "id == %@", id)

request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [filters])

let results = (try? context.fetch(request)) ?? []

You can add more NSPredicate's to filters if you wish.