Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Support NSFetchedPropertyDescription #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions MTLManagedObjectAdapter/MTLManagedObjectAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ FOUNDATION_EXPORT const unsigned char MTLManagedObjectAdapterVersionString[];
// be used.
+ (NSDictionary *)relationshipModelClassesByPropertyKey;

// similar with relationshipModelClassesByPropertyKey
+ (NSDictionary *)fetchedPropertyModelClassesByPropertyKey;

// Overridden to deserialize a different class instead of the receiver, based on
// information in the provided object.
//
Expand Down
34 changes: 31 additions & 3 deletions MTLManagedObjectAdapter/MTLManagedObjectAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#import <Mantle/Mantle.h>

#import "EXTScope.h"
#import "EXTRuntimeExtensions.h"
#import "mtl_moa_EXTScope.h"
#import "mtl_moa_EXTRuntimeExtensions.h"

#import "MTLManagedObjectAdapter.h"

Expand Down Expand Up @@ -64,6 +64,9 @@ @interface MTLManagedObjectAdapter ()
// A cached copy of the return value of +relationshipModelClassesByPropertyKey.
@property (nonatomic, copy, readonly) NSDictionary *relationshipModelClassesByPropertyKey;

// A cached copy of the return value of +fetchedPropertyModelClassesByPropertyKey.
@property (nonatomic, copy, readonly) NSDictionary *fetchedPropertyModelClassesByPropertyKey;

// A cache of the return value of -valueTransformersForModelClass:
@property (nonatomic, copy, readonly) NSDictionary *valueTransformersByPropertyKey;

Expand Down Expand Up @@ -144,6 +147,10 @@ - (id)initWithModelClass:(Class)modelClass {
if ([modelClass respondsToSelector:@selector(relationshipModelClassesByPropertyKey)]) {
_relationshipModelClassesByPropertyKey = [[modelClass relationshipModelClassesByPropertyKey] copy];
}

if ([modelClass respondsToSelector:@selector(fetchedPropertyModelClassesByPropertyKey)]) {
_fetchedPropertyModelClassesByPropertyKey = [[modelClass fetchedPropertyModelClassesByPropertyKey] copy];
}

return self;
}
Expand Down Expand Up @@ -200,6 +207,25 @@ - (id)modelFromManagedObject:(NSManagedObject *)managedObject processedObjects:(

return setValueForKey(propertyKey, value);
};

BOOL (^deserializeFetchedProperty)(NSFetchedPropertyDescription *) = ^(NSFetchedPropertyDescription *fetchedPropertyDescription) {
Class nestedClass = self.fetchedPropertyModelClassesByPropertyKey[propertyKey];
if (nestedClass == nil) {
[NSException raise:NSInvalidArgumentException format:@"No class specified for decoding relationship at key \"%@\" in managed object %@", managedObjectKey, managedObject];
}
id models = performInContext(context, ^id{
NSArray *fetchedArray = [managedObject valueForKey:managedObjectKey];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:[fetchedArray count]];
for (NSManagedObject *nestedObject in fetchedArray) {
id<MTLManagedObjectSerializing> model = [self.class modelOfClass:nestedClass fromManagedObject:nestedObject error:error];
[models addObject:model];
}

return models;
});

return setValueForKey(propertyKey, models);
};

BOOL (^deserializeRelationship)(NSRelationshipDescription *) = ^(NSRelationshipDescription *relationshipDescription) {
Class nestedClass = self.relationshipModelClassesByPropertyKey[propertyKey];
Expand Down Expand Up @@ -262,7 +288,9 @@ - (id)modelFromManagedObject:(NSManagedObject *)managedObject processedObjects:(
return deserializeAttribute((id)propertyDescription);
} else if ([propertyClassName isEqual:@"NSRelationshipDescription"]) {
return deserializeRelationship((id)propertyDescription);
} else {
} else if ([propertyClassName isEqualToString:@"NSFetchedPropertyDescription"]) {
return deserializeFetchedProperty((id)propertyDescription);
} else {
if (error != NULL) {
NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"Property descriptions of class %@ are unsupported.", @""), propertyClassName];

Expand Down