0
votes

I'm struggling with a CoreData related problem for a while now:

I have a View-based application with a tabbar in it. The first two tabs are view controllers that display content from my CoreData, the third tab has a UIPickerView, where I can select data. The third and the second view controllers then should select the right entity based on the UIPickerView selection.

To clarify this a little bit more: in my managedobjectcontext I have 16 entities. Based on the UIPickerView selection in ThirdViewController I want to use the corresponding entity and update the FirstViewController and SecondViewController.

This should also be a persistent solution, so that the user can quit the application, but the selection from UIPickerView should be stored. Maybe this is something for NSUserDefaults?

Any help would be greatly appreciated!

1
I'm not clear on what you asking. Are you asking how to save something to Core Data in general? Are you asking if you should be using Core Data as opposed to user defaults? Are you asking how to pass a particular managed object to another view controller? - TechZen
Oh, I'm sorry for causing a little confusion. I don't want to save something to Core Data. To clarify this one more time: I have 3 ViewControllers: ViewController #1: shows data from Core Data ViewController #2: shows data from Core Data ViewController #3: has a UIPickerView that let's the user select one entity with its attributs from Core Data. - Tobias Frischholz
What I want is the following: When the user chooses an entity from ViewController #3 this information should be persistently stored, so that ViewController #1 and #2 always know (even when the user quits the app - no backgrounding!), from which entity they should get the data. Did I made myself clear? - Tobias Frischholz
You say "entity" but I think you mean a specific managed object. Entities are to managed objects what classes are to instances. E.g. You might a have a Person entity in your data model to describe a person i.e. firstName,lastName, but a specific individual, say "Bob Smith" would described by a particular managed object initialized to the Person entity. Which do you need to persist from ViewController#3. - TechZen

1 Answers

0
votes

I am not exactly clear on whether you need to save an entity name or a particular managed object but in both cases the solution is the same: you need to store a reference in the NSUserDefaults.

To store an entity in the user defaults you would just save the entity name as a string. So something like:

  [[NSUserDefaults standardUserDefaults] setValue:entityNameString forKey:@"currentlySelectedEntity"];

... and to retrieve it:

  NSString *currentEntity=[[[NSUserDefaults standardUserDefaults] valueForKey:@"currentlySelectedEntity"];

If you need to save a reference to a particular managed object, you need to save the managed object ID of the saved object. Saving is very important. Prior to saving the context in which the object was created, the object ID has only a temporary value which will change upon saving. Any reference to object will be lost if you use the temporary value.

To save an object ID:

  NSManagedObject *mo=[NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext];
  //... save the context
  NSManagedObjectID *moID=[mo objectID];
  NSURL *moIDURI=[moID URIRepresentation];
  [[NSUserDefaults standardUserDefaults] setValue:moIDURI forKey:@"currentlySelectedTestObject"];

... to retrive it:

  NSURL *uri=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentlySelectedTestObject"];
  NSManagedObjectID *moID=[self.persistentStoreCoordinator managedObjectIDForURIRepresentation:uri];  
  NSManagedObject *mo=[self.managedObjectContext objectRegisteredForID:moID];

(The above code is uncompiled and may contain typos so don't just copy and paste it.)