user1107173
user1107173

Reputation: 10744

Saving and Deleting 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.

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];
    }

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

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.

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.

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.

Upvotes: 3

Views: 5195

Answers (1)

Rakesh
Rakesh

Reputation: 3399

  1. To delete you just need to delete letsMeet object from NSManagedObjectContext.

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

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.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];

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

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.

Update: To get the objectId of a NSManagedObject:

 [letsMeet objectId];

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.

Upvotes: 3

Related Questions