Access FetchRequest from xcdatamodel in iOS
Asked Answered
K

1

6

I'm using NSPersistentContainer to access my core data in iOS10 app and Xcode 8. Similar to Entity, I added a "UserFetchRequest" in my Coredata xcdatamodel. Below is the screen shot.

Screen Shot

How can I access the "UserFetchRequest" in the code?

Kary answered 30/12, 2016 at 5:51 Comment(0)
L
6

Having given your fetch request a name in the Data Model Inspector on the right hand side:

Data Model Editor

(in my case named "randomFetch" where yours is "UserFetchRequest"), get the model from the persistentContainer:

let model = persistentContainer.managedObjectModel

and then access the fetch request using the fetchRequestTemplate(forName:) method:

let fetchRequest = model.fetchRequestTemplate(forName: "randomFetch") as! NSFetchRequest<Event>

You can then execute this fetch request as normal.

Lag answered 30/12, 2016 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.