forked from RestKit/RestKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRKManagedObjectImporter.m
More file actions
302 lines (251 loc) · 12.6 KB
/
Copy pathRKManagedObjectImporter.m
File metadata and controls
302 lines (251 loc) · 12.6 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// RKManagedObjectImporter.m
// RestKit
//
// Created by Blake Watters on 3/4/10.
// 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.
//
#if TARGET_OS_IPHONE
#import <MobileCoreServices/UTType.h>
#endif
#import "RKManagedObjectImporter.h"
#import "RKMapperOperation.h"
#import "RKManagedObjectMappingOperationDataSource.h"
#import "RKInMemoryManagedObjectCache.h"
#import "RKMIMETypeSerialization.h"
#import "RKPathUtilities.h"
#import "RKLog.h"
// Set Logging Component
#undef RKLogComponent
#define RKLogComponent RKlcl_cRestKitCoreData
@interface RKManagedObjectImporter ()
@property (nonatomic, strong, readwrite) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readwrite) NSString *storePath;
@property (nonatomic, strong, readwrite) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, strong, readwrite) NSPersistentStore *persistentStore;
@property (nonatomic, strong, readwrite) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readwrite) RKManagedObjectMappingOperationDataSource *mappingOperationDataSource;
@property (nonatomic, strong, readwrite) NSOperationQueue *connectionQueue;
@property (nonatomic, assign) BOOL hasPerformedResetIfNecessary;
@end
@implementation RKManagedObjectImporter
- (id)initWithManagedObjectModel:(NSManagedObjectModel *)managedObjectModel storePath:(NSString *)storePath
{
NSParameterAssert(managedObjectModel);
NSParameterAssert(storePath);
self = [super init];
if (self) {
self.managedObjectModel = managedObjectModel;
self.storePath = storePath;
NSError *error = nil;
NSPersistentStoreCoordinator *persistentStoreCoordinator = [self createPersistentStoreCoordinator:&error];
NSAssert(persistentStoreCoordinator, @"Importer initialization failed: Unable to create persistent store coordinator: %@", error);
self.persistentStoreCoordinator = persistentStoreCoordinator;
NSManagedObjectContext *managedObjectContext = [self createManagedObjectContext];
NSAssert(managedObjectContext, @"Importer initialization failed: Unable to create managed object context");
self.managedObjectContext = managedObjectContext;
self.connectionQueue = [NSOperationQueue new];
[self.connectionQueue setName:@"RKManagedObjectImporter Connection Queue"];
[self.connectionQueue setSuspended:YES];
RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [self createMappingOperationDataSource];
self.mappingOperationDataSource = mappingOperationDataSource;
self.hasPerformedResetIfNecessary = NO;
self.resetsStoreBeforeImporting = YES;
}
return self;
}
- (id)initWithPersistentStore:(NSPersistentStore *)persistentStore
{
NSParameterAssert(persistentStore);
self = [super init];
if (self) {
self.persistentStoreCoordinator = persistentStore.persistentStoreCoordinator;
self.managedObjectModel = persistentStore.persistentStoreCoordinator.managedObjectModel;
NSURL *storeURL = [self.persistentStoreCoordinator URLForPersistentStore:persistentStore];
self.storePath = [storeURL path];
NSManagedObjectContext *managedObjectContext = [self createManagedObjectContext];
NSAssert(managedObjectContext, @"Importer initialization failed: Unable to create managed object store");
self.managedObjectContext = managedObjectContext;
self.connectionQueue = [NSOperationQueue new];
[self.connectionQueue setName:@"RKManagedObjectImporter Connection Queue"];
[self.connectionQueue setSuspended:YES];
RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [self createMappingOperationDataSource];
self.mappingOperationDataSource = mappingOperationDataSource;
self.hasPerformedResetIfNecessary = NO;
self.resetsStoreBeforeImporting = NO;
}
return self;
}
- (id)init
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer. Invoke initWithManagedObjectModel:storePath: instead.",
NSStringFromClass([self class])]
userInfo:nil];
}
- (NSPersistentStoreCoordinator *)createPersistentStoreCoordinator:(NSError **)error
{
BOOL isDirectory = NO;
[[NSFileManager defaultManager] fileExistsAtPath:self.storePath isDirectory:&isDirectory];
NSAssert(!isDirectory, @"Cannot create SQLite persistent store: The given store path specifies a directory.");
NSURL *storeURL = [NSURL fileURLWithPath:self.storePath];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSPersistentStore *persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil error:error];
if (! persistentStore) {
return nil;
}
return persistentStoreCoordinator;
}
- (NSManagedObjectContext *)createManagedObjectContext
{
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[managedObjectContext performBlockAndWait:^{
managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
}];
return managedObjectContext;
}
- (RKManagedObjectMappingOperationDataSource *)createMappingOperationDataSource
{
NSAssert(self.connectionQueue, @"Connection Queue cannot be nil");
RKInMemoryManagedObjectCache *managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.managedObjectContext];
RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:self.managedObjectContext
cache:managedObjectCache];
mappingOperationDataSource.operationQueue = self.connectionQueue;
return mappingOperationDataSource;
}
- (void)resetPersistentStoreIfNecessary
{
if (self.hasPerformedResetIfNecessary) return;
if (self.resetsStoreBeforeImporting) {
RKLogInfo(@"Persistent store reset requested before importing. Deleting existing managed object instances...");
for (NSEntityDescription *entity in self.managedObjectModel.entities) {
@autoreleasepool {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = entity;
[self.managedObjectContext performBlockAndWait:^{
NSError *error;
NSArray *managedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
RKLogInfo(@"Deleting %ld managed object instances for the '%@' entity", (unsigned long) [managedObjects count], entity.name);
for (NSManagedObject *managedObject in managedObjects) {
[self.managedObjectContext deleteObject:managedObject];
}
}];
}
}
}
self.hasPerformedResetIfNecessary = YES;
}
- (NSUInteger)importObjectsFromFileAtPath:(NSString *)path withMapping:(RKMapping *)mapping keyPath:(NSString *)keyPath error:(NSError **)error
{
NSParameterAssert(path);
NSParameterAssert(mapping);
// Perform the reset on the first import action if requested
[self resetPersistentStoreIfNecessary];
__block NSError *localError = nil;
NSData *payload = [NSData dataWithContentsOfFile:path options:0 error:&localError];
if (! payload) {
RKLogError(@"Failed to read file at path '%@': %@", path, [localError localizedDescription]);
if (error) *error = localError;
return NSNotFound;
}
NSString *MIMEType = RKMIMETypeFromPathExtension(path);
id parsedData = [RKMIMETypeSerialization objectFromData:payload MIMEType:MIMEType error:&localError];
if (!parsedData) {
RKLogError(@"Failed to parse file at path '%@': %@", path, [localError localizedDescription]);
}
if (! parsedData) {
if (error) *error = localError;
return NSNotFound;
}
NSDictionary *mappingDictionary = @{ (keyPath ?: [NSNull null]) : mapping };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingDictionary];
mapper.mappingOperationDataSource = self.mappingOperationDataSource;
self.mappingOperationDataSource.parentOperation = mapper;
__block RKMappingResult *mappingResult;
[self.managedObjectContext performBlockAndWait:^{
[mapper start];
mappingResult = mapper.mappingResult;
localError = mapper.error;
}];
if (mappingResult == nil) {
if (error) *error = localError;
RKLogError(@"Importing file at path '%@' failed with error: %@", path, localError);
return NSNotFound;
}
NSUInteger objectCount = [mappingResult count];
RKLogInfo(@"Imported %lu objects from file at path '%@'", (unsigned long)objectCount, path);
return objectCount;
}
- (NSUInteger)importObjectsFromDirectoryAtPath:(NSString *)path withMapping:(RKMapping *)mapping keyPath:(NSString *)keyPath error:(NSError **)error
{
NSError *localError = nil;
NSArray *entries = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&localError];
if (! entries) {
RKLogError(@"Import failed for directory at path '%@': Unable to read directory contents with error: %@", path, localError);
if (error) *error = localError;
return NSNotFound;
}
NSUInteger aggregateObjectCount = 0;
for (NSString *entry in entries) {
NSUInteger objectCount = [self importObjectsFromFileAtPath:path withMapping:mapping keyPath:keyPath error:&localError];
if (objectCount == NSNotFound) {
if (error) *error = localError;
return NSNotFound;
} else {
aggregateObjectCount += objectCount;
}
}
return aggregateObjectCount;
}
- (NSUInteger)importObjectsFromItemAtPath:(NSString *)path withMapping:(RKMapping *)mapping keyPath:(NSString *)keyPath error:(NSError **)error
{
NSParameterAssert(path);
NSParameterAssert(mapping);
BOOL isDirectory;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
if (isDirectory) {
return [self importObjectsFromDirectoryAtPath:path withMapping:mapping keyPath:keyPath error:error];
}
return [self importObjectsFromFileAtPath:path withMapping:mapping keyPath:keyPath error:error];
}
- (BOOL)finishImporting:(NSError **)error
{
// Perform our connection operations in a batch, before we save the MOC
[self.connectionQueue setSuspended:NO];
[self.connectionQueue waitUntilAllOperationsAreFinished];
__block BOOL success;
__block NSError *localError = nil;
[self.managedObjectContext performBlockAndWait:^{
success = [self.managedObjectContext save:&localError];
if (! success) {
RKLogCoreDataError(localError);
}
}];
if (! success && error) *error = localError;
return success;
}
- (void)logSeedingInfo
{
NSString *storeDirectory = [self.storePath stringByDeletingLastPathComponent];
NSString *storeFilename = [self.storePath lastPathComponent];
RKLogCritical(@"A seed database has been generated at '%@'. "
@"Please execute `open \"%@\"` in your Terminal and copy %@ to your app. Be sure to add the seed database to your \"Copy Resources\" build phase.",
self.storePath, storeDirectory, storeFilename);
}
@end