3

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.

user1107173
  • 10,334
  • 16
  • 72
  • 117

1 Answers1

3
  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.

Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • Thanks Rakesh. Its late here and I will give this a shot in the AM. As I continue to build the application, I will add more objects. I'll let you know in the AM. By the way, I still haven't figured out how to get the objectID yet. – user1107173 Feb 28 '13 at 04:59
  • As for #3, I want to keep saving the objects as they are completed and pull them up in UItableview based on Timestamp. So when you say I will only have one object, do you mean one object in managedObjectContext? May be I'm confusing the two. The current object will the object the user will be working on objectArray[0]. The other objects will be the objects that will be saved as the user completes the form and shown as a history log in UItableview. I hope I'm making sense. Thanks. – user1107173 Feb 28 '13 at 05:53
  • You can only access your core-data objects through the managedObjectContext. After you execute the fetch request resultArray[0] will be the only object in the array and that will be the object you want to fetch. According to your comment rather than a formId/objectId you will be using the timestamp to execute the fetch. To set predicate by date look here http://stackoverflow.com/questions/1965331/nspredicate-filtering-objects-by-day-of-nsdate-property. – Rakesh Feb 28 '13 at 11:50
  • I have updated the answer to include more explanation about the managed object context. But from your questions i feel you are not familiar with some basic concepts of core-data maybe you should go through the apple reference first. – Rakesh Feb 28 '13 at 12:08
  • Thanks for the explanation. I'm working on my 2nd Core Data book. I understand the concepts but just need some clarification. I did a count of the objects in my array, and there were 56! Even though on each run I may have completed the entire form twice. Thus, I need to persist ONLY when the user saves the form and get delete the object if the user does not finish the complete finishing the form. But then I fall back to my old question is how do I access the same NSManagedObject from scene to scene. – user1107173 Feb 28 '13 at 20:21
  • I would like to learn more about Core Data from you on gChat or Skype if possible and wouldn't mind paying you for your time. Let me know such arrangement interests you? Thanks. – user1107173 Feb 28 '13 at 20:22
  • I can explain whatever I understand via chat. No problem. No need to pay me or anything :D. But you can get the basic references and even good video tutorials over the net. I think the problem is we are in two very different time zones. – Rakesh Mar 01 '13 at 02:25
  • Let's Chat. Did you add me on GChat? – user1107173 Mar 01 '13 at 15:46
  • @user1107173: btw stackoverflow also has a chat. :) – Rakesh Mar 03 '13 at 17:38
  • Thanks I saw that! I'm tied up with until Thursday. I'll message you then. THanks. – user1107173 Mar 04 '13 at 19:17