在Core Data fetched属性表达式中,显然$FETCH_SOURCE
解析为对象ID 而不是对象本身。这引起了一个例外“类不是键值编码......”。这是真的,因为它是响应该键的对象。
我想将源对象的属性值用作fetched属性谓词的一部分。
知道如何解决这个问题吗?
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_NSObjectID_48_0 0x10101b200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key messageType.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff92ba00c6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8f85e3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff92c34249 -[NSException raise] + 9
3 Foundation 0x00007fff9518b08c -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 238
4 Foundation 0x00007fff950d2df9 -[NSObject(NSKeyValueCoding) valueForKey:] + 400
5 Foundation 0x00007fff950f36a8 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 341
6 Foundation 0x00007fff950a3489 -[NSFunctionExpression expressionValueWithObject:context:] + 751
7 CoreData 0x00007fff88cbbdda -[NSSQLSimpleWhereIntermediate initWithPredicate:inScope:] + 618
8 CoreData 0x00007fff88cbb672 -[NSSQLGenerator generateWhereIntermediatesInContext:] + 162
9 CoreData 0x00007fff88cbb010 -[NSSQLGenerator generateIntermediatesForFetchInContext:countOnly:] + 416
10 CoreData 0x00007fff88cb7d30 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 400
11 CoreData 0x00007fff88cb7a97 -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 471
12 CoreData 0x00007fff88cb76b6 -[NSSQLCore newRowsForFetchPlan:] + 118
13 CoreData 0x00007fff88cb6f2e -[NSSQLCore objectsForFetchRequest:inContext:] + 526
14 CoreData 0x00007fff88cb6a91 -[NSSQLCore executeRequest:withContext:error:] + 225
15 CoreData 0x00007fff88cb5f72 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 2114
16 CoreData 0x00007fff88cb4309 -[NSManagedObjectContext executeFetchRequest:error:] + 537
17 CoreData 0x00007fff88cfca4e -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 414
18 CoreData 0x00007fff88d3072a __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke_0 + 634
19 libdispatch.dylib 0x00007fff8b2710b6 _dispatch_client_callout + 8
20 libdispatch.dylib 0x00007fff8b272723 _dispatch_barrier_sync_f_invoke + 39
21 CoreData 0x00007fff88cfc87c _perform + 172
22 CoreData 0x00007fff88cfc6c2 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 354
23 CoreData 0x00007fff88cb4309 -[NSManagedObjectContext executeFetchRequest:error:] + 537
24 CoreData 0x00007fff88d0481e -[NSFaultHandler retainedFulfillAggregateFaultForObject:andRelationship:withContext:] + 254
25 CoreData 0x00007fff88d7faa8 -[_NSFaultingMutableArray willRead] + 120
26 CoreData 0x00007fff88d7fe99 -[_NSFaultingMutableArray count] + 25
27 CoreFoundation 0x00007fff92b82e69 -[NSArray lastObject] + 25
28 Scuttlebutt 0x0000000100041aab -[BSMessageCellView setObjectValue:] + 283
29 AppKit 0x00007fff936ca006 -[NSTableRowData _addViewToRowView:atColumn:row:] + 696
30 AppKit 0x00007fff936c9b5f -[NSTableRowData _addViewsToRowView:atRow:] + 151
31 AppKit 0x00007fff936c80f5 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
32 AppKit 0x00007fff936c7e7a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
33 AppKit 0x00007fff936c7149 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
34 AppKit 0x00007fff936c6ce1 -[NSTableRowData updateVisibleRowViews] + 119
35 AppKit 0x00007fff936da8b3 -[NSTableRowData _idleUpdateVisibleRows] + 66
36 CoreFoundation 0x00007fff92b5cdc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
37 CoreFoundation 0x00007fff92b5c8dd __CFRunLoopDoTimer + 557
38 CoreFoundation 0x00007fff92b420c9 __CFRunLoopRun + 1513
39 CoreFoundation 0x00007fff92b416e2 CFRunLoopRunSpecific + 290
40 HIToolbox 0x00007fff9427e0a4 RunCurrentEventLoopInMode + 209
41 HIToolbox 0x00007fff9427de42 ReceiveNextEventCommon + 356
42 HIToolbox 0x00007fff9427dcd3 BlockUntilNextEventMatchingListInMode + 62
43 AppKit 0x00007fff93544b13 _DPSNextEvent + 685
44 AppKit 0x00007fff935443d2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
45 AppKit 0x00007fff9353b773 -[NSApplication run] + 517
46 AppKit 0x00007fff934e01a6 NSApplicationMain + 869
47 Scuttlebutt 0x0000000100002225 main + 53
48 Scuttlebutt 0x00000001000021e4 start + 52
49 ??? 0x0000000000000003 0x0 + 3
)
当我尝试转储违规对象的细节时,会出现这种情况 - 很明显它是一个对象ID而不是对象本身。
(lldb) expression (NSString*) [(id)0x10101b200 description]
(NSString *) $0 = 0x0000000108b03880 @"0x10101b200 <x-coredata://8F1FBB6B-505B-4169-A9D0-10D48CE5D4DC/YammerMessage/p101>"
(lldb) expression (Class) [(id)0x10101b200 class]
(Class) $2 = _NSObjectID_48_0
答案 0 :(得分:2)
您可以使用setResultType:方法设置NSFetchRequest的结果类型,以获取NSManagedObjects,您必须将其设置为NSManagedObjectResultType。
NSFetchRequest *fetchrequest = [[NSFetchRequest alloc] init];
[fetchRequest setResultType:NSManagedObjectResultType];
如果您使用XCode的CoreData编辑器,您可以通过选择获取请求并导航到右侧实用程序选项卡中的“数据模型Inspektor”选项卡来选择结果类型。
修改强> 见screenshot
答案 1 :(得分:2)
你是绝对正确的。 $ FETCH_SOURCE解析为NSManagedObjectID,所以像$ FETCH_SOURCE.myProperty这样的东西会引发异常(即使Apple的文档使用这种表示法)。
我通过在我的项目中添加以下类别来解决它,该项目捕获了调用并将其转发到真实对象:
@implementation NSManagedObjectID (FetchFix)
- (id)valueForUndefinedKey:(NSString *)key {
// Try to get the real object
NSManagedObject *object = [[NSManagedObjectContext defaultContext] objectWithID:self];
return [object valueForKey:key];
}
@end
P.S。我正在使用MagicalRecord,因此使用defaultContext
快捷方式。在代码中使用适当的MOC。
答案 2 :(得分:0)
这已在iOS的未来版本中修复(我提交了一个错误。)
答案 3 :(得分:0)
似乎在iOS 11和XCode 9中还没有修复bug。我最终放弃了获取的属性,而是使用了计算属性。毕竟,他们使用相同的基础BinaryFormatter
。
以下是一个示例代码段:
NSFetchRequest