I'm facing a big issue with fetched properties.
I have this entities in the model:
EntityDetail
Attributes:
NSString: idd_info
[...]
Fetched Property:
Name: info Destination: EntityInfo Predicate: idd==$FETCH_SOURCE.idd_info
EntityInfo
Attributes:
NSString: idd
NSString: title
[...]
In this way I can obtain related EntityInfo when I need:
[[detail.info objectAtIndex:0] title]
I prefer this approach instead of configuring a regular relationship due to performance reasons during the parsing process.
If I use the Apple generated CoreData boilerplate to setup the stack, everything works fine and I can use the fetched property with success. But if I use the RestKit provided implementation by the RKManagedObjectStore class, the fetched property does not work. This is the error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_NSObjectID_48_15 0x1a2f9ac0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key idd_info.'
It seems that $FETCH_SOURCE resolves to a NSObjectID instead of the NSObject itself. And, obviously, the idd_info property is not defined on that class.
Googling around I found the possibility to setup the resultType on a fetchRequest to NSManagedObjectResultType. This is the default behavior as stated in Apple documentation. So why does RestKit 0.20.3 use NSManagedObjectIDResultType instead? With RestKit 0.10 everything works fine...
Is this a know bug? How can I force the fetched property to NSManagedObjectResultType ?
This is the way I setup the persistent stores and create managed object context using RKManagedObjectStore. I am in the application delegate and _managedObjectModel _managedObjectStore _managedObjectContext are singleton variables used in the whole app).
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"momd"]];
_managedObjectModel= [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
_managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:_managedObjectModel];
[_managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [[self docDir] stringByAppendingPathComponent::@"MyApp.sqlite"];
NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
NSPersistentStore __unused *persistentStore = [_managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:options error:&error];
NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
[_managedObjectStore createManagedObjectContexts];
[RKManagedObjectStore setDefaultStore:_managedObjectStore];
[_managedObjectStore.mainQueueManagedObjectContext setUndoManager:nil];
_managedObjectContext = _managedObjectStore.mainQueueManagedObjectContext;
Thank you.
UPDATE 1:
If I use:
_managedObjectContext = _managedObjectStore.persistentStoreManagedObjectContext;
instead of:
_managedObjectContext = _managedObjectStore.mainQueueManagedObjectContext;
in the application delegate at startup the issue doesn't occurs and fetched property works fine, but it still occurs during mapping.
This is how I set up the mapping.
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];
[RKObjectManager setSharedManager:objectManager];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:appDeleg.managedObjectModel];
[objectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{ NSInferMappingModelAutomaticallyOption: @YES,NSMigratePersistentStoresAutomaticallyOption: @YES, NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"} } error:nil];
[objectStore createManagedObjectContexts];
objectManager.managedObjectStore=objectStore;
[objectManager.HTTPClient setAuthorizationHeaderWithUsername:usernameString password:passwordString]
[... here RKEntityMappings and ResponseDescriptors added to the objectManager ...]
[[RKObjectManager sharedManager] getObjectsAtPath:remoteBaseAddedPath parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[delegate parserDidSuccessParsingData:self];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[delegate parserDidFailParsingData:self withError:error];
}];
UPDATE 2:
This is the stack trace during mapping. Please note that I have a transient property on the object which uses the fetched property in the getter, RestKit call this getter and crash occurs:
2014-03-20 11:10:53.545 MyApp[15378:f03] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
2014-03-20 11:10:53.549 MyApp[15378:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSTemporaryObjectID_default 0xcb9a870> valueForUndefinedKey:]: this class is not key value coding-compliant for the key idd_info.'
*** First throw call stack:
(
0 CoreFoundation 0x02b021e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x028818e5 objc_exception_throw + 44
2 CoreFoundation 0x02b91fe1 -[NSException raise] + 17
3 Foundation 0x00f6dc7a -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x00edadfd _NSGetUsingKeyValueGetter + 81
5 Foundation 0x00eda437 -[NSObject(NSKeyValueCoding) valueForKey:] + 260
6 Foundation 0x00ef9f0a -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 409
7 Foundation 0x00eab465 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
8 CoreData 0x00728bbc -[NSSQLSimpleWhereIntermediate initWithPredicate:inScope:] + 620
9 CoreData 0x007283eb -[NSSQLGenerator generateWhereIntermediatesInContext:] + 187
10 CoreData 0x00727dc1 -[NSSQLGenerator generateIntermediatesForFetchInContext:countOnly:] + 673
11 CoreData 0x007242f3 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 483
12 CoreData 0x00723fb8 -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 472
13 CoreData 0x00723dd0 -[NSSQLAdapter newSelectStatementWithFetchRequest:] + 48
14 CoreData 0x00723b91 -[NSSQLCore newRowsForFetchPlan:] + 129
15 CoreData 0x0072331d -[NSSQLCore objectsForFetchRequest:inContext:] + 701
16 CoreData 0x00722dcf -[NSSQLCore executeRequest:withContext:error:] + 383
17 CoreData 0x007227f2 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4466
18 CoreData 0x0071ff56 -[NSManagedObjectContext executeFetchRequest:error:] + 566
19 CoreData 0x00774d86 -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 502
20 CoreData 0x007f2a14 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 676
21 CoreData 0x00774b81 internalBlockToNSManagedObjectContextPerform + 17
22 libdispatch.dylib 0x032394d0 _dispatch_client_callout + 14
23 libdispatch.dylib 0x03226740 _dispatch_barrier_sync_f_invoke + 58
24 libdispatch.dylib 0x032263ea dispatch_barrier_sync_f + 89
25 CoreData 0x00774b02 _perform + 114
26 CoreData 0x007749ae -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 238
27 CoreData 0x0071ff56 -[NSManagedObjectContext executeFetchRequest:error:] + 566
28 CoreData 0x00774d86 -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 502
29 CoreData 0x007f2a14 __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 676
30 CoreData 0x00774b81 internalBlockToNSManagedObjectContextPerform + 17
31 libdispatch.dylib 0x032394d0 _dispatch_client_callout + 14
32 libdispatch.dylib 0x03228439 _dispatch_barrier_sync_f_slow_invoke + 80
33 libdispatch.dylib 0x032394d0 _dispatch_client_callout + 14
34 libdispatch.dylib 0x03227726 _dispatch_main_queue_callback_4CF + 340
35 CoreFoundation 0x02b6743e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
36 CoreFoundation 0x02aa85cb __CFRunLoopRun + 1963
37 CoreFoundation 0x02aa79d3 CFRunLoopRunSpecific + 467
38 CoreFoundation 0x02aa77eb CFRunLoopRunInMode + 123
39 GraphicsServices 0x0392c5ee GSEventRunModal + 192
40 GraphicsServices 0x0392c42b GSEventRun + 104
41 UIKit 0x012bff9b UIApplicationMain + 1225
42 MyApp 0x001a186c main + 76
43 libdyld.dylib 0x0346e701 start + 1
44 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException