I need to serialize my NSURL.
object is type of NSManagedObject.
NSURL *objectURIRepresentation = [[object objectID] URIRepresentation];
NSError *error = nil;
NSData *objectIDData = [NSData dataWithContentsOfURL:objectURIRepresentation options:NSDataReadingMapped error:&error];
I get error: (Cocoa error 256.).
Any ideas? Something tells me, using dataWithContentsOfURL:
is not good idea.
Update
One more question which is put as a comment mistakenly:
What is the difference between [NSData dataWithContentsOfURL:uri];
and [NSKeyedArchiver archivedDataWithRootObject:uri];
?
Thanks.
Firstly, your code does not attempt to serialize a NSURL object, it attempts to create a data object out of the data at the URL returned as the URI of a managed object.
Secondly, that is never going to work.
[NSData dataWithContentsOfURL:]
will try to read a file at a particular URL. The URI of a managed object represents an object stored in pieces with many others inside a persistent file like a SQLite database.
The URI only allows a managed object context to identify a particular object in its own store. The URI is gibberish to anything else other than the context.
NSManagedObject does not implement the NSCoder protocol so managed objects cannot be serialized. I'm not sure what you want to do here but you can't do it this way.
As mentioned here Cocoa error 256 core data
error code 256 can occur when an unknown error is occurred in reading the resource or the path has some encoded characters in it.
What it seems to me is you are trying to get the data from NSManagedObject
. Hence as @fluchtpunkt suggested you should look for http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html
Now coming to your second question [NSData dataWithContentsOfURL:uri];
returns data for a web url or a local resource in your documents directory. While [NSKeyedArchiver archivedDataWithRootObject:uri];
returns the NSData
object containing the encoded form of the object graph whose root object is given.
try to add "Http://" before in the link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With