I'm trying to retrieve and create a managed object from a NSURL saved in my user-defaults, but I can't figure out how to use the managedObjectIDForURIRepresentation-function. My code looks like this:
let defaults = NSUserDefaults.standardUserDefaults()
let usedKeyName = "timerList"
let usedList = defaults.objectForKey(usedKeyName) as? [NSURL] ?? 
let url:NSURL = usedList as NSURL
let managedobject:NSManagedObjectID = //What do I write here?
Any suggestions would be appreciated.
Best How To :
To obtain an
NSManagedObjectID from the URI representation, you must call the appropriate method on the
The URI representation includes the UUID of the store used to persist the object. Calling
managedObjectIDForURIRepresentation: will cause Core Data to attempt to find the correct store based on that information, and then use the rest of the URI to locate that specific object. Because of this make sure your store's UUID is not changing every time you start up. If Core Data cannot find the data associated with the URI representation
managedObjectIDForURIRepresentation: will return nil.
So, for instance, you would do something like:
NSManagedObjectID objectId = [[managedObjectContext persistentStoreCoordinator] managedObjectIDForURIRepresentation:uri];
Or in swift:
let objectId:NSManagedObjectID = persistentStoreCoordinator.managedObjectIDForURIRepresentation(uri)