从iOS中的xcdatamodel访问FetchRequest [英] Access FetchRequest from xcdatamodel in iOS
问题描述
我正在使用NSPersistentContainer访问iOS10应用程序和Xcode 8中的核心数据.与实体类似,我在Coredata xcdatamodel中添加了"UserFetchRequest".下面是屏幕截图.
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.
如何访问代码中的"UserFetchRequest"?
How can I access the "UserFetchRequest" in the code?
推荐答案
在右侧的数据模型检查器中为您的提取请求指定了名称:
Having given your fetch request a name in the Data Model Inspector on the right hand side:
(在我的情况下为"randomFetch",其中您的名称为"UserFetchRequest"),从persistentContainer获取模型:
(in my case named "randomFetch" where yours is "UserFetchRequest"), get the model from the persistentContainer:
let model = persistentContainer.managedObjectModel
,然后使用 fetchRequestTemplate(forName:)
方法访问提取请求:
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.
这篇关于从iOS中的xcdatamodel访问FetchRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!