What is the correct way to store NSURL in the underlying data?

I am trying to archive / unpack NSManagedObjectIDs in Core Data objects, so the next time my application starts, I can get these identifiers and use them to retrieve specific objects.

I tried to "archive" the ID, for example:

//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 unarchive like this (I just want [anyObject] because I am testing and there is only one):

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

But when I try to get the url as 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 object was set using Xcode to "transformable", and I left the transformer value field in Xcode empty, since the Core Data documentation seems to imply that it uses the default transformer empty.

What am I doing wrong?

+5
4

... 14 .. ... ... :

ArchivedID. :

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

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

, URL-. , , .

URL -absoluteString .

+1

. , CoreData. , CoreData - . .

Why not just have an object in CoreData that supports a one-to-many relationship with the objects you like? Then you can just insert / delete / search / iterate / regardless of the collection of objects.

0
source

I like what @kevboh suggested. You can also consider the preservation of objectIDconcrete NSManagedObjectin NSUserDefaults. For instance:.

[[NSUserDefaults standardUserDefaults] setObject:featuredNSManagedObjectIDArray
                                          forKey:@"FeaturedNSManagedObjectIDs"];
-1
source

All Articles