Structuring Core Data

Using a relationship I am able to pair entries together, but cannot figure out how to exactly match the customer that I am looking for, nor to create new records without overwriting the old ones.

Code Block
let userEntryRecord = NSEntityDescription.entity(forEntityName: “Record”, in: managedContext)!
let userEntryCustomer = NSEntityDescription.entity(forEntityName: “Customer”, in: managedContext)!
let dataRecord = NSManagedObject(entity: userEntryRecord, insertInto: managedContext) as? Record
let dataCustomer = NSManagedObject(entity: userEntryCustomer, insertInto: managedContext) as? Customer
dataRecord?.year = “2021”
dataRecord?.month = “March”
dataRecord?.addToCustomer(dataCustomer ?? Customer())
dataCustomer?.age = 20
dataCustomer?.gender = “male”
dataCustomer?.country = “UK


I would like to search for all records for October 2023, with those of ages between 20-40, how do I create a fetch request for that ?

On top of that, I need to amend records, and of course create new years to attach records to, something I am having difficulty with.

Replies

Look into NSPredicate syntax.
Post not yet marked as solved Up vote reply of awal Down vote reply of awal
@forklift 
You should define multi criteria predicate:


See detailed explanations here
https:// code.tutsplus. com/tutorials/core-data-and-swift-relationships-and-more-fetching--cms-25070