Created
May 24, 2013 15:19
-
-
Save anonymous/5644255 to your computer and use it in GitHub Desktop.
NSCoder implementations for preserving NSManagedObject ObjectIDs as URLs and restoring those objects from a managed object context.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation ACodableManagedObject | |
+ (id)ObjectWithManagedObject:(NSManagedObject*)obj | |
{ | |
ACodableManagedObject* codableObj = nil; | |
if (nil != obj) | |
{ | |
codableObj = [[ACodableManagedObject alloc] init]; | |
codableObj.url = obj.objectID.URIRepresentation; | |
} | |
return codableObj; | |
} | |
- (id)initWithCoder:(NSCoder*)aDecoder | |
{ | |
self = [super init]; | |
if (nil != self) | |
{ | |
self.url = [aDecoder decodeObjectForKey:@"url"]; | |
} | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder*)aCoder | |
{ | |
[aCoder encodeObject:self.url forKey:@"url"]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation AManagedObjectArchiver | |
+ (NSData*)ArchiveDataFromBlock:(void(^)(AManagedObjectArchiver* archiver))codingBlock | |
{ | |
NSMutableData* data = [NSMutableData data]; | |
AManagedObjectArchiver* archiver = | |
[[AManagedObjectArchiver alloc] initForWritingWithMutableData:data]; | |
codingBlock(archiver); | |
[archiver finishEncoding]; | |
return data; | |
} | |
- (void)encodeObject:(id)obj forKey:(NSString*)key | |
{ | |
if ([obj isKindOfClass:[NSManagedObject class]]) | |
{ | |
ACodableManagedObject* codableObj = | |
[ACodableManagedObject ObjectWithManagedObject:obj]; | |
[self encodeObject:codableObj forKey:key]; | |
} | |
else | |
{ | |
[super encodeObject:obj forKey:key]; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)UnarchiveData:(NSData*)data | |
Context:(NSManagedObjectContext*)context | |
Block:(void(^)(AManagedObjectUnarchiver* unarchiver))decodingBlock | |
{ | |
AManagedObjectUnarchiver* unarchiver = [[AManagedObjectUnarchiver alloc] initForReadingWithData:data]; | |
unarchiver.context = context; | |
decodingBlock(unarchiver); | |
[unarchiver finishDecoding]; | |
} | |
- (id)decodeObjectForKey:(NSString*)key | |
{ | |
id obj = [super decodeObjectForKey:key]; | |
if ([obj isKindOfClass:[ACodableManagedObject class]]) | |
{ | |
ACodableManagedObject* codableObj = obj; | |
NSURL* url = codableObj.url; | |
NSManagedObjectID* objectId = | |
[self.context.persistentStoreCoordinator | |
managedObjectIDForURIRepresentation:url]; | |
obj = [self.context existingObjectWithID:objectId error:NULL]; | |
} | |
return obj; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment