Closed edwardmp closed 9 years ago
Your NSManagedObject subclass need conform to NSCoding protocol, which means implement:
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
Thanks for the reply. Yes that is what I did (the StackOverflow post mentioned that).
Code I used for this:
- (id)initWithCoder:(NSCoder *)decoder {
NSManagedObjectContext *context = [[CoreDataManager sharedManager] managedObjectContext]; // use your NSManagedObjectContext
NSPersistentStoreCoordinator *coordinator = [context persistentStoreCoordinator]; //use your NSPersistentStoreCoordinator
NSURL *url = (NSURL *)[decoder decodeObjectForKey:@"URIRepresentation"];
NSManagedObjectID *managedObjectID = [coordinator managedObjectIDForURIRepresentation:url];
self = (Flight* )[context existingObjectWithID:managedObjectID error:nil];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:[[self objectID] URIRepresentation] forKey:@"URIRepresentation"];
}
Yeah, that's more or less the best you can do. As a word of warning, I'd be careful using a generic MOC that way. That can work, but it can also be an anti-pattern as far as Core Data is concerned. In general, I'd recommend simply passing the Managed Object ID representations through as plain strings and having a different object on the other side that is capable of fetching managed objects for those IDs with a specific context. It's more code, but it's a little more deliberate, which with Core Data can be a good thing in the long run. Just food for thought.
First of all thank you for publishing this framework, it should be very useful for communicating with Apple Watch extensions.
I am trying to receive notifications regarding Core Data updates, as suggested in this thread: http://stackoverflow.com/questions/27791594/watchkit-core-data-sync-up/29475315#29475315
As soon as MMWormhole sends a message from iOS, the receiving Watch app crashes on the following line:
id messageObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
It has something to with unarchiving an object (the Core Data ManagedObject I assume)
Full crash report: