/
RKPropertyInspector+CoreData.m
161 lines (140 loc) · 7.45 KB
/
RKPropertyInspector+CoreData.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// RKPropertyInspector+CoreData.m
// RestKit
//
// Created by Blake Watters on 8/14/11.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <CoreData/CoreData.h>
#import <objc/message.h>
#import "RKPropertyInspector+CoreData.h"
#import "RKLog.h"
#import "RKObjectUtilities.h"
#import "RKMacros.h"
// Set Logging Component
#undef RKLogComponent
#define RKLogComponent RKlcl_cRestKitCoreData
@interface RKPropertyInspector ()
@property (nonatomic, assign) dispatch_queue_t queue;
@property (nonatomic, strong) NSMutableDictionary *inspectionCache;
@end
@implementation RKPropertyInspector (CoreData)
- (NSDictionary *)propertyInspectionForEntity:(NSEntityDescription *)entity
{
__block NSMutableDictionary *entityInspection;
dispatch_sync(self.queue, ^{
entityInspection = [self.inspectionCache objectForKey:[entity name]];
});
if (entityInspection) return entityInspection;
entityInspection = [NSMutableDictionary dictionary];
for (NSString *name in [entity attributesByName]) {
NSAttributeDescription *attributeDescription = [[entity attributesByName] valueForKey:name];
if ([attributeDescription attributeValueClassName]) {
Class cls = NSClassFromString([attributeDescription attributeValueClassName]);
if ([cls isSubclassOfClass:[NSNumber class]] && [attributeDescription attributeType] == NSBooleanAttributeType) {
cls = objc_getClass("NSCFBoolean") ?: objc_getClass("__NSCFBoolean") ?: cls;
}
RKPropertyInspectorPropertyInfo *info;
info = [RKPropertyInspectorPropertyInfo propertyInfoWithName:name
keyValueClass:cls
isPrimitive:NO];
[entityInspection setValue:info forKey:name];
} else if ([attributeDescription attributeType] == NSTransformableAttributeType &&
![name isEqualToString:@"_mapkit_hasPanoramaID"]) {
const char *className = [[entity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding];
const char *propertyName = [name cStringUsingEncoding:NSUTF8StringEncoding];
Class managedObjectClass = objc_getClass(className);
objc_property_t prop = class_getProperty(managedObjectClass, propertyName);
// Property is not defined in the Core Data model -- we cannot infer any details about the destination type
if (prop) {
const char *attr = property_getAttributes(prop);
Class destinationClass = RKKeyValueCodingClassFromPropertyAttributes(attr);
if (destinationClass) {
RKPropertyInspectorPropertyInfo *info;
info = [RKPropertyInspectorPropertyInfo propertyInfoWithName:name
keyValueClass:destinationClass
isPrimitive:NO];
[entityInspection setObject:info forKey:name];
}
}
}
}
for (NSString *name in [entity relationshipsByName]) {
NSRelationshipDescription *relationshipDescription = [[entity relationshipsByName] valueForKey:name];
if ([relationshipDescription isToMany]) {
if ([relationshipDescription isOrdered]) {
RKPropertyInspectorPropertyInfo *info;
info = [RKPropertyInspectorPropertyInfo propertyInfoWithName:name
keyValueClass:[NSOrderedSet class]
isPrimitive:NO];
[entityInspection setObject:info forKey:name];
} else {
RKPropertyInspectorPropertyInfo *info;
info = [RKPropertyInspectorPropertyInfo propertyInfoWithName:name
keyValueClass:[NSSet class]
isPrimitive:NO];
[entityInspection setObject:info forKey:name];
}
} else {
NSEntityDescription *destinationEntity = [relationshipDescription destinationEntity];
Class destinationClass = NSClassFromString([destinationEntity managedObjectClassName]);
if (! destinationClass) {
RKLogWarning(@"Retrieved `Nil` value for class named '%@': This likely indicates that the class is invalid or does not exist in the current target.", [destinationEntity managedObjectClassName]);
}
RKPropertyInspectorPropertyInfo *info;
info = [RKPropertyInspectorPropertyInfo propertyInfoWithName:name
keyValueClass:destinationClass ?: [NSNull null]
isPrimitive:NO];
[entityInspection setObject:info forKey:name];
}
}
dispatch_barrier_async(self.queue, ^{
[self.inspectionCache setObject:entityInspection forKey:[entity name]];
RKLogDebug(@"Cached property inspection for Entity '%@': %@", entity, entityInspection);
});
return entityInspection;
}
- (Class)classForPropertyNamed:(NSString *)propertyName ofEntity:(NSEntityDescription *)entity
{
NSDictionary *entityInspection = [self propertyInspectionForEntity:entity];
RKPropertyInspectorPropertyInfo *propertyInspection = [entityInspection objectForKey:propertyName];
return propertyInspection.keyValueCodingClass;
}
@end
@interface NSManagedObject (RKPropertyInspection)
- (Class)rk_classForPropertyAtKeyPath:(NSString *)keyPath isPrimitive:(BOOL *)isPrimitive;
@end
@implementation NSManagedObject (RKPropertyInspection)
- (Class)rk_classForPropertyAtKeyPath:(NSString *)keyPath isPrimitive:(BOOL *)isPrimitive
{
NSRange dotRange = [keyPath rangeOfString:@"." options:NSLiteralSearch];
RKPropertyInspector *inspector = [RKPropertyInspector sharedInspector];
Class currentPropertyClass = [self class];
Class propertyClass = nil;
if (dotRange.length == 0) {
propertyClass = [inspector classForPropertyNamed:keyPath ofEntity:[self entity]];
return propertyClass ?: [inspector classForPropertyNamed:keyPath ofClass:currentPropertyClass isPrimitive:isPrimitive];
}
NSArray *components = [keyPath componentsSeparatedByString:@"."];
for (NSString *property in components) {
if (isPrimitive) *isPrimitive = NO; // Core Data does not enable you to model primitives
propertyClass = [inspector classForPropertyNamed:property ofEntity:[self entity]];
propertyClass = propertyClass ?: [inspector classForPropertyNamed:property ofClass:currentPropertyClass isPrimitive:isPrimitive];
if (! propertyClass) break;
currentPropertyClass = propertyClass;
}
return propertyClass;
}
@end