从核心数据中检索数据
我使用 xcdatamodel 定义许多基于 CoreData 数据实体的类。这非常有效,我可以根据Apple的示例检索它们:
然而,我所追求的是一种打包的方法将方法获取到另一个类中,但我有几个问题...... 例如
MyDataAccessClass *mdac = [[MyDataAccessClass alloc] init]; myFetchedData = [mdac fetchData];
Q1.如果我这样做,在类中定义 NSManagedObjectContext 可以吗?还是仍然需要在我的视图控制器中引用它并传递给我的“MyDataAccessClass”? Q2。对我来说,在 XCode 为数据模型中的实体创建的类中设置数据检索方法是有意义的。虽然每次我尝试这样做,然后自动更新这些类,因为它们是由 XCode 自动生成的,但它们会覆盖我定义的任何方法。
提前致谢!
I'm using an xcdatamodel to define a number of classes based upon CoreData data entities. This is working great and I can retrieve them in accordance to Apple's examples:
What I'm after however, is a way to package the fetch method up into another class, but I have a couple of questions...
e.g.
MyDataAccessClass *mdac = [[MyDataAccessClass alloc] init];
myFetchedData = [mdac fetchData];
Q1. If I do this, is it ok that the NSManagedObjectContext is defined in the class? or does it still need to be referenced in my view controller and passed to my 'MyDataAccessClass'?
Q2. It makes sense to me to have the data retrieval methods set up in the classes created by XCode for the entities in the data model. Although every time I try to do this, then update those classes automatically because they are automatically generated by XCode, they overwrite any methods I've defined.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也许能够在单独的类中创建一个新的 NSManagedObjectContext - 不确定是否会出现任何问题,因为已经在 appDelegate 中创建了一个。我所做的就是您在第一季度第二部分中所建议的,其中我将 NSManageObjectContext 传递到单独的方法类中,这样我就可以执行以下操作:
myFetchedData = [mdac fetchData:currentNSManagedObjectContext];
You might be able to create a new NSManagedObjectContext in the seperate class - not sure if there will be any issues with that since one is already created in the appDelegate. What I did was what you suggest in the second part of Q1, where I pass the NSManageObjectContext into the seperate method class so then I can do something like:
myFetchedData = [mdac fetchData:currentNSManagedObjectContext];