Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the correct way to store an NSURL in Core Data?

I'm trying to archive / unarchive NSManagedObjectIDs in Core Data objects so that next time my app starts up I can retrieve these IDs and use them to fetch specific objects.

I tried "archiving" the ID like this:

//defaultConfiguration is an NSManagedObject defined elsewhere and it works just fine...    
// and newObject is also properly initialized, bound to a context,etc...

ArchivedID* newID = [NSEntityDescription insertNewObjectForEntityForName:@"ArchivedID" inManagedObjectContext:managedObjectContext];
[self.defaultConfiguration addArchiveIDObject:newID];
newID.idURI = [[newObject objectID] URIRepresentation];
[managedObjectContext save:&error]; 

And then unarchiving like this (I'm just going for [anyObject] cause i'm testing and there's only one at this point):

NSManagedObjectID* ID = [managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:[defaultConfiguration.archiveID anyObject]];

But when I try getting the URL back like above, I get the following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ArchivedID relativeString]: unrecognized selector sent to instance 0x7f59f20'

The attribute in the entity was set up through Xcode to "transformable" and I left the transformer value field in Xcode empty since the Core Data documentation seems to imply if empty it's use the default transformer.

What am I doing wrong?

like image 336
SaldaVonSchwartz Avatar asked Nov 25 '22 00:11

SaldaVonSchwartz


1 Answers

It's possible you can solve this problem without storing URLs. Consider adding a boolean flag to your model and marking the objects you wish to retrieve as true, then fetch flagged objects next time your app starts.

However, you could try getting the string version of the URL with -absoluteString and storing that.

like image 130
kevboh Avatar answered Dec 10 '22 21:12

kevboh