1

I'm developing an app that uses CoreData. For the purpose of my app, I would like to save the managedObjectId of an object (in a plist file) in order to retrieve this object on the next app launch.

Following some reseaches, I tried :

myDictionary setObject:self.myPerson.objectID.URIRepresentation.absoluteString forKey:@"lastSelectedPerson"];
// Then I write the dictonary into a file
// I get a value like "x-coredata://8CF33004-BADD-402D-9AA5-115A030F901A/Person/p1"

myPerson is a object that has been save in the managedObjectContext.

Next I have tried to retrieve the objectID, but I get a nil value with this code :

// After retrieving the dictonary from the plist file
NSString * lastSelectedPersonId = [myDictionary objectForKey:@"lastSelectedPerson"];
NSManagedObjectID * objectID = [self.managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:[NSURL URLWithString:lastSelectedPersonId]];
self.myPerson = (Person *)[self.managedObjectContext existingObjectWithID:objectID error:&error];

Do you have any suggestion ? Thanks.

yeesterbunny
  • 1,847
  • 2
  • 13
  • 17
Maxime Capelle
  • 887
  • 7
  • 16
  • What is myDictionary in the 2nd part of your code? Is it the same dictionary from the first part? Or is it a new dictionary initialized by contents of the plist that was saved locally? – yeesterbunny Jun 30 '13 at 19:50
  • That's a new dictonary loaded with the content of the plist. `myDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathToThePListFile];` – Maxime Capelle Jun 30 '13 at 19:57
  • Check the plist that you saved locally. Is the value for that key nil? – yeesterbunny Jun 30 '13 at 19:59
  • In the plist, I have got a value like `x-coredata://8CF33004-BADD-402D-9AA5-115A030F901A/Person/p1`. I also retrieve this value correctly in the second part of the code. – Maxime Capelle Jun 30 '13 at 20:13
  • So `lastSelectedPersonId` is the same value that you saved, but `objectID` is nil? I tested the code in a small sample project where it worked without problems. – Martin R Jun 30 '13 at 20:59
  • Hum ... My test was as following : 1 - I write the `objectId` in the file. 2 - I close and kill the app. 3 - I launch the app to retrieve the good `objectId`. – Maxime Capelle Jul 01 '13 at 14:11
  • Ok, I'll try to implement that in a small project. By the way, is it the good way to answer my original need or maybe do you know a better method ? – Maxime Capelle Jul 01 '13 at 16:00

2 Answers2

1

To restore object on next launch you can alternatively try following:

  1. Save objectID to NSUserDefaults:

    [[NSUserDefaults standardUserDefaults] setObject:self.myPerson.objectID forKey:@"lastSelectedPerson"]; 
    [[NSUserDefaults standardUserDefaults] synchronize];
    
  2. On the next launch get value from NSUserDefaults:

    NSNumber *lastSelectedPersonID = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastSelectedPerson"];
    
  3. Query Core Data store to get object:

    - (Person *)personWithID:(NSNumber *)personID {
    
    Person *person = nil;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    request.predicate = [NSPredicate predicateWithFormat:@"objectID == %@", personID];
    
    NSError *error = nil;
    NSArray *persons = [self.managedObjectContext executeFetchRequest:request error:&error];
    
    person = [persons lastObject];
    
    return person;
    }
    
Numeral
  • 1,405
  • 12
  • 24
0

I had a similar problem that I was creating a managed object and pulling off the object id and it hadn't been saved to disk yet. If that is the case the object id is a "temporary" id. I would check using: [NSManagedObjectID isTemporaryID]

If it returns that it is a temporary id you need to save the object and make sure you have a permanent id.

Documentation: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectID_Class/Reference/NSManagedObjectID.html#//apple_ref/occ/instm/NSManagedObjectID/isTemporaryID

utahwithak
  • 6,235
  • 2
  • 40
  • 62