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

查看:194
本文介绍了保存和删除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。即用户具有他正在场景之间填充的形式。当用户点击导航控制器上的下一步按钮时,每个场景的所有内容都保存到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. 要删除只需删除 letsMeet 对象从 NSManagedObjectContext

NSError * error; code>

[managedObjectContext deleteObject:letsMeet];

[managedObjectContext save :& error];

对象,获取 letsMeet 的引用不是问题。您可以像在代码中所做的一样。

更新:
您不需要删除托管对象上下文。它只是一个空间来处理你的对象。更多解释在问题的结尾。

royalty
2.如果 LetsMeet 实体的建模方式是所有表单元素都是<$创建一个 LetsMeet 对象后,保存 managedObjectContext 你已经在代码中完成,这将被保存为一个单一的对象。

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&amp; NSManagedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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