Access FetchRequest from xcdatamodel in iOS

拟墨画扇 提交于 2021-01-27 06:31:40

问题


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.

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


回答1:


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

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



来源:https://stackoverflow.com/questions/41391310/access-fetchrequest-from-xcdatamodel-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!