保存和删除 NSManagedObject &NSManagedObjectContext [英] Saving and Deleting NSManagedObject & NSManagedObjectContext

查看:17
本文介绍了保存和删除 NSManagedObject &NSManagedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

三个问题,但它们都是相关的.如果你喜欢我可以把它们分成三个问题,这样你就可以获得更多的学分.如果你想让我这样做,请告诉我.

Three Questions but they are all related. If you like I can divide them into three questions so that you can more credits. Let me know if you'd like for me to do that.

我有以下代码允许我访问 NSManagedObject

I have the following code that allows me to access NSManagedObject

self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication]  delegate] managedObjectContext];

NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]];
NSArray *objectArray = [managedObjectContext executeFetchRequest:request error:&error];
if(objectArray.count==0){
    letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext];
    } else{
    letsMeet = (LetsMeet *)[objectArray objectAtIndex:0];
    }

上面的代码允许我保存和检索属性.即我可以访问letsMeet.attribute 来保存和获取.

The code above allows me to save and retrieve attributes. i.e. I can access letsMeet.attribute to save and fetch.

问题 1:如何删除并启动一个全新的 managedObjectContext.即用户有一个他在场景之间填写的表格.当用户点击导航控制器上的 Next 按钮时,每个场景的所有内容都会保存到 CoreData.经过几个屏幕后,用户想要取消表单.在这一点上,我想删除到目前为止已保存的所有内容.请提供代码示例.

Question 1: How do I delete and start a brand new managedObjectContext. i.e. User has a form that he's been filling out between the scenes. Everything is saved to CoreData from each scene as the user hits the Next button on the navigation Controller. After going through several screens, the user wants to cancel the form. At this point I would like to delete everything that has been saved thus far. Code example please.

问题 2:假设用户接近表单末尾并决定保存表单以供以后检索.如何将整个表单的副本保存为 Core Data 中的一个对象.请提供代码示例.

Question 2: Lets say the user gets towards to end of the form and decides to save the form for later retrieval. How do I save a copy of the entire form as one object in Core Data. Code example please.

问题 3:我以后如何从 Core Data 中检索保存的对象并显示所有用户保存的内容?请提供代码示例.

Question 3: How do I retrieve that saved object from Core Data at a later time and display what all the user had saved? Code example please.

推荐答案

  1. 要删除你只需要从 NSManagedObjectContext 中删除 letsMeet 对象.

NSError *error;
[managedObjectContext deleteObject:letsMeet];
[managedObjectContext save:&error];

由于您始终只有一个对象,因此获取 letsMeet 的引用不是问题.您可以像在代码中那样做.
更新:而且您不需要删除托管对象上下文.它只是一个处理您的对象的空间.问题末尾有更多解释.

2. 如果 LetsMeet 实体的建模方式是所有表单元素都是 LetsMeet 的属性,当你在创建一个 managedObjectContext 后保存LetsMeet 对象,就像您在代码中所做的那样,这将被保存为单个对象.

Since you always have only one object, getting the reference of letsMeet is not a problem. You can do as you did in your code.
Update: And you don't need to delete the managed object context. It just a space to deal with your objects. More explanation at the end of question.

2. If the LetsMeet entity is modeled in a way that all the form elements are attributes of LetsMeet, when you save the managedObjectContext after creating a LetsMeet object as you have done in code, this will be saved as a single object.

3.您已经知道如何检索对象,因为这就是您在代码中所做的事情.一切都变得简单,因为您只使用一个对象.在多个对象获取唯一对象的情况下,您应该实现一个主键,(可能是formID,即;向LetsMeet添加另一个属性)或者您应该知道每个对象的objectId是什么是,然后相应地设置您的提取请求的谓词.

3.You already know how to retrieve an object as thats what you are doing in the code. Everything becomes easy as you are only using one object. In the case of multiple objects to get a the unique object, you should either implement a primary key,(maybe formID, i.e; add another attribute to LetsMeet) or you should know what the objectId of each object is and then set the predicate of your fetch request accordingly.

NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:letsMeet];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"formId like %@", formId];
[request setPredicate:predicate];

NSArray *resultsArray =[managedObjectContext executeFetchRequest:request error:&error];

如果您的 formId 是唯一的,这将返回一个对象数组.

If your formId is unique, this will return you a single object array.

但如果您使用 core-data 仅处理一个对象,您可以使用 NSUserDefaults 或写入 plist 文件来执行此操作.这有点矫枉过正.

But if you are using core-data for only handling one object, you could've used NSUserDefaults or write to a plist File to do this. This is kind of overkill.

更新:获取 NSManagedObject 的 objectId:

Update: To get the objectId of a NSManagedObject:

 [letsMeet objectId];

ManagedObjectContext 就像一块白板.您在数组中拥有的对象,在托管对象上下文中的对象,都是一样的.您可以更改对象、添加对象、删除对象等.只有当您执行 [managedObjectContext save:] 时,对象的当前状态是什么,即写入磁盘.

ManagedObjectContext is like a whiteboard. The object you have inside the array, the object inside managed object context, its all the same. You can change the objects, add object, delete object etc. Only thing is whatever is the current state of the object(s) when you do a [managedObjectContext save:] , that is written to disk.

这篇关于保存和删除 NSManagedObject &NSManagedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆