Nikolay Borisov Nikolay Borisov - 1 year ago 62
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download