11

在 Core Data 获取的属性表达式中,显然$FETCH_SOURCE解析为对象 ID而不是对象本身。这导致了异常“类不是键值编码......”。这是真的,因为它是响应该键的对象。

我想将源对象的属性值用作获取的属性谓词的一部分。

知道如何解决这个问题吗?

*** 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
4

4 回答 4

2

您可以使用 setResultType: 方法设置 NSFetchRequest 的结果类型,要获取 NSManagedObjects,您必须将其设置为 NSManagedObjectResultType。

NSFetchRequest *fetchrequest = [[NSFetchRequest alloc] init];
[fetchRequest setResultType:NSManagedObjectResultType];

如果您使用 XCode 的 CoreData 编辑器,您可以通过选择获取请求并导航到右侧实用程序选项卡中的“数据模型检查器”选项卡来选择结果类型。

编辑: 看这个截图

于 2013-08-02T22:07:03.670 回答
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

PS我正在使用MagicalRecord,因此是defaultContext快捷方式。在您的代码中使用适当的 MOC。

于 2014-08-11T10:09:25.240 回答
0

这已在 iOS 的未来版本中得到修复(我提交了一个错误。)

于 2013-08-26T21:32:17.847 回答
0

似乎 iOS 11 和 XCode 9 中的 bug 尚未修复。我最终放弃了 fetched 属性,而是使用了计算属性。NSFetchRequest毕竟他们使用相同的底层证券。

这是一个示例代码段:

public class Sales: NSManagedObject {

}

extension Sale {

    var detailList: [SaleDetail] {
        guard let id = self.id else {
            return []
        }

        let predicate = NSPredicate(format: "sale_id == '\(id)'")

        let fetchRequest: NSFetchRequest<SaleDetail> = SaleDetail.fetchRequest()
        fetchRequest.predicate = predicate
        do {
            if let list = try self.managedObjectContext?.fetch(request) {
                return list
            } else {
                return []
            }
        } catch let error {
            print("error:\(error)")
            return []
        }
    }
}
于 2018-05-18T03:56:18.777 回答