Pergunta

I have a core data entity with a placemark data attribute in which I'm trying to store a CLPlacemark object.

@interface ZJPlace : NSManagedObject
...     
@property (nonatomic, retain) NSData * placemark;

I'm encoding the object like this:

place.placemark = [NSKeyedArchiver archivedDataWithRootObject:self.placemark];

and decoding like this:

self.placemark = [NSKeyedUnarchiver unarchiveObjectWithData:place.placemark];

On decoding, I'm not getting an error or nil back - it just looks like an empty CLPlacemark object (self.placemark 's class is CLPlaceMark but description doesn't print anything to the log.)

I can see there's data saved in the place.placemark core data attribute but it's not getting decoded back to a proper object. It's basically the same problem as in this question: Trouble decoding with NSKeyedUnarchiver which went unanswered.

Is there something fundamental I'm missing here about how to use NSKeyedArchiver and CoreData together? Thanks in advance for any hints...

Foi útil?

Solução

Durrr... of course there was something fundamental I had completely forgotten about how Core Data stores complex objects. Hope this helps some other newbie out there who's as clueless as I was. Sigh.

Docs: Transformable attributes

So here's the really simple solution to my question above about encoding a CLPlacemark in core data:

  1. change type of attribute in Core Data model to be Transformable
  2. In the header for the NSManagedObject subclass, remember to change the type of the NSManagedObject's property from NSData to whatever class you're trying to encode, in my case CLPlacemark

Then just use the attribute as you would a normal object, e.g.

self.placemark = place.placemark

No mucking around with NSKeyedArchivers needed.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top