2012-09-06 11 views
11

In un'espressione di proprietà recuperata con Dati core, apparentemente $FETCH_SOURCE risolve un ID oggetto anziché l'oggetto stesso. Ciò ha causato un'eccezione "la classe non è la codifica del valore-chiave ...". Il che è vero poiché è l'oggetto che risponde a quella chiave.

Vorrei utilizzare i valori delle proprietà dell'oggetto di origine come parte del predicato di una proprietà recuperata.

Qualche idea su come risolvere questo problema?

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

Questo è ciò che è venuto quando ho provato il dumping dettagli dell'oggetto incriminato - ben visibile che si tratta di un ID oggetto invece dell'oggetto stesso.

(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

Assicurati di poter accedere alle proprietà del tuo $ FETCH_SOURCE nel tuo predicato. Sono certo di averlo. Controllare i documenti [https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW7]. Puoi pubblicare il tuo predicato? –

+0

Sto affrontando lo stesso problema e sembra essere correlato alla creazione di contesti usando l'API RestKit. Se utilizzo lo stack normale di coredata, tutto funziona come dovrebbe. Hai trovato una soluzione? – Donnit

risposta

1

È possibile impostare il tipo di risultato di un NSFetchRequest con il metodo setResultType :, per ottenere NSManagedObjects, è necessario impostarlo su NSManagedObjectResultType.

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

Se si utilizza l'editor CoreData di XCode, è possibile scegliere il tipo di risultato selezionando la richiesta di recupero e la navigazione a tab 'Data Model Inspektor' nella scheda a destra utilità.

Edit: Vai a questa screenshot

+0

Selezione della proprietà recuperata nell'editor Coredata Posso solo impostare le coppie Nome, Destinazione, Predicato e valore-chiave del dizionario utenteinfo. Come scegli il tipo di risultato? Grazie. – Donnit

+0

Ho aggiunto uno screenshot alla mia risposta – MeXx

0

Questo è stato risolto in una versione futura di iOS (ho presentato un bug.)

+0

Potresti elaborare un po '? È un bug iOS confermato? –

+0

I (e altri nei forum degli sviluppatori Apple) mi sono imbattuto nello stesso problema in iOS 7.1 - Ho inserito il bug # 16697979. – Rupert

+0

È una correzione per confermare? Btw, è un'app per OS X. – adib

3

Hai assolutamente ragione. $ FETCH_SOURCE si risolve in NSManagedObjectID quindi qualcosa come $ FETCH_SOURCE.myProperty genererà un'eccezione (anche se la documentazione di Apple utilizza questa notazione).

ho risolto con l'aggiunta della categoria al mio progetto, che cattura la chiamata e l'inoltra alla oggetto reale:

@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. Sto usando MagicalRecord, da cui il collegamento defaultContext. Utilizzare un MOC appropriato nel codice.

+0

Funzionerà solo se si dispone di un singolo contesto, ad esempio un record magico.Non funzionerà su app più complesse con più stack di dati core e quindi su più contesti che potrebbero essere attivi. Inoltre un ID oggetto gestito è generalmente privo di contesto. – adib

+0

Hai ragione. È pensato come una soluzione rapida, non una soluzione universale. – frenya

+0

Hai salvato la nostra giornata :), grazie mille. –

Problemi correlati