From 544a5fbaf354dbeafc4031e135300fdced28025c Mon Sep 17 00:00:00 2001 From: Six Date: Tue, 20 Sep 2016 17:30:15 +0800 Subject: [PATCH 1/2] support fetched property --- .DS_Store | Bin 0 -> 8196 bytes .../MTLManagedObjectAdapter.h | 3 ++ .../MTLManagedObjectAdapter.m | 34 ++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0ca71ec5bbe7cf17cef3c355e12a10fc456080c4 GIT binary patch literal 8196 zcmeI1&ubGw6vyABv2L0M_E0gP!iq_7V(5(Oz0YJmb zRL^5`N@4S?&XtTDSwSV}4`4%pwF?y7Icqhb2q*%IfFhs>C<6Zi0lc$W(ayN{#jI)- z0Y%`yB*4xG11nQgYATc;9oX;`05yxlykH;m0PbT*HKnFPxhuvL*#nWTM5Y)-xZ}LR z%u!9LsZik#M7RTyD-)Ta5WYJ48QdI*DO9zJfFdwYfaUJj;W0E}8{66ZeFq%a$4tHh z2|U1j-i878C`8UU9yosJ;7fMH8TekDl*?a8W+{7R`Kp#%$*&gj1${MN$Un7u;n+&- zWEj`%sIA|$JeN9lwClM^uStU~pN?p-6K&fGjh-;$W*GWtAo{k`^60p&uRRQd#P$Lj z@pvM}-mVw8;kXtK12^8A4cykRH6l7f*M=R?mKS@D=X=Q^4=N3LzhNgmTujrCa$2sf z7bla-=4Q#Lu2-ieW3sVPE*bZ$>(i;0)3jT6AGMsr7e}vNzde5U{=>(w-@Xf$M4m^m z_8Z$D=Lb8oqQDP>E_$-4x6ijNa1Wm2W`(#_6t^nE-oD?hSe>vx5YP7M&0?bV!+PBv zCLMs!%P+;byMSlyhwcSL9rR`hh`qY=M_k9qGlBz%k*$rK=}ETa7}+i;@+o{57I}{G zf+C;bjCVMqHrS^Q)s&hF6`P>$e?~(UMd0!XEQzIyEdSp+`}hCLdqB0aBA^IdI02Ha zTlE?)Bd+H8^)fhQV10&_6=s(TN literal 0 HcmV?d00001 diff --git a/MTLManagedObjectAdapter/MTLManagedObjectAdapter.h b/MTLManagedObjectAdapter/MTLManagedObjectAdapter.h index dfefb20..7060d6a 100644 --- a/MTLManagedObjectAdapter/MTLManagedObjectAdapter.h +++ b/MTLManagedObjectAdapter/MTLManagedObjectAdapter.h @@ -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. // diff --git a/MTLManagedObjectAdapter/MTLManagedObjectAdapter.m b/MTLManagedObjectAdapter/MTLManagedObjectAdapter.m index 3a3189f..d1c645e 100644 --- a/MTLManagedObjectAdapter/MTLManagedObjectAdapter.m +++ b/MTLManagedObjectAdapter/MTLManagedObjectAdapter.m @@ -10,8 +10,8 @@ #import -#import "EXTScope.h" -#import "EXTRuntimeExtensions.h" +#import "mtl_moa_EXTScope.h" +#import "mtl_moa_EXTRuntimeExtensions.h" #import "MTLManagedObjectAdapter.h" @@ -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; @@ -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; } @@ -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 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]; @@ -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]; From 221c70704a33703d933dbdeaca95533d6a3281b2 Mon Sep 17 00:00:00 2001 From: ondev Date: Tue, 20 Sep 2016 17:41:10 +0800 Subject: [PATCH 2/2] f --- .DS_Store | Bin 8196 -> 8196 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 0ca71ec5bbe7cf17cef3c355e12a10fc456080c4..ab24215d79ad09087b5705181809b85c253f4b8f 100644 GIT binary patch delta 260 zcmZp1XmOa}FUrHfz`)4BAi$7RUR;orlb;0SN^dNj#y+uucQZQ&3kNSq1q*{7Lpnny zL&;=2fm`fG1{OLB#wL^H1wBRKGG)O5`HI6wfwqAx1o{9BgrPKyItFx!(dOxbXP7n?RWog7 Um-xmqSwUEJ@=xKcjYSnq01&o8n*aa+ delta 80 zcmZp1XmOa}FUrNhz`)4BAi%(o$dCub=?tk1DH{u?u}^H^-OSFx!okQjxlhPwvV_2` k$xcFIlQ{&fHcu4#%``bjX5z*QVdl;365m)hR