Nikolay Borisov Nikolay Borisov - 10 months ago 46
Swift Question

Core Data 3 read and write data from index

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
cards = try context.fetch(Card.fetchRequest()).

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 Source
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.