2015-04-15 93 views
0

當我在IOS 8.3執行這行代碼這一行崩潰:iOS的碰撞與核心數據

NSArray *results = [self executeFetchRequest:request inContext:context]; 

這裏是失敗的全部功能:

+ (id) executeFetchRequestAndReturnFirstObject:(NSFetchRequest *)request inContext:(NSManagedObjectContext *)context 
{ 
    [request setFetchLimit:1]; 

    NSArray *results = [self executeFetchRequest:request inContext:context]; 
    if ([results count] == 0) 
    { 
     return nil; 
    } 
    return [results objectAtIndex:0]; 
} 

上述函數的調用:

+ (id)findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context 
{ 
    NSFetchRequest *request = [self requestFirstByAttribute:attribute withValue:searchValue inContext:context]; 
    [request setPropertiesToFetch:[NSArray arrayWithObject:attribute]]; 

    return [self executeFetchRequestAndReturnFirstObject:request inContext:context]; 
} 

值得注意的是,我非常新到iOS編程,所以請去容易對我來說,這個問題可能不是最好的,可能沒有太多的後盾代碼,但這是由於我在iOS中缺乏知識。我的Android的問題通常比這更好的:P如果我能夠添加任何更多的細節,我會盡我所能增加更多,崩潰日誌低於

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Bad fetch request (NSManagedObjectResultType not compatible with contents of propertiesToFetch)' * First throw call stack: ( 0 CoreFoundation 0x0000000111d7dc65 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000110fafbb7 objc_exception_throw + 45 2 CoreData
0x00000001112ecb6e -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:] + 1646 3 CoreData 0x00000001112d8dc4 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 244 4 CoreData 0x00000001111f0e0c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316 5 CoreData 0x00000001111f0a86 -[NSSQLCore newRowsForFetchPlan:] + 118 6 CoreData 0x00000001111f033c -[NSSQLCore objectsForFetchRequest:inContext:] + 524 7 CoreData 0x00000001111efdbb -[NSSQLCore executeRequest:withContext:error:] + 299 8 CoreData 0x00000001112caa6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356 9 CoreData
0x00000001112d3c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192 10 libdispatch.dylib 0x0000000114e29614 _dispatch_client_callout + 8 11 libdispatch.dylib 0x0000000114e0f002 _dispatch_barrier_sync_f_invoke + 365 12 CoreData 0x00000001112c5245 _perform + 197 13 CoreData
0x00000001111efa58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504 14 CoreData
0x00000001111ee2ca -[NSManagedObjectContext executeFetchRequest:error:] + 586 15 HawkExpress
0x000000010e52438e +[NSManagedObject(MagicalRecord) executeFetchRequest:inContext:] + 62 16 HawkExpress
0x000000010e52446e +[NSManagedObject(MagicalRecord) executeFetchRequestAndReturnFirstObject:inContext:] + 78 17 HawkExpress 0x000000010e5261fc +[NSManagedObject(MagicalRecord) findFirstByAttribute:withValue:inContext:] + 140 18 HawkExpress
0x000000010e526274 +[NSManagedObject(MagicalRecord) findFirstByAttribute:withValue:] + 100 19 HawkExpress
0x000000010e52aa54 +[FavouritesManager doesFavouriteExist:] + 84 20 HawkExpress 0x000000010e4de4a7 -[BookCabViewController tableView:cellForRowAtIndexPath:] + 2087 21 UIKit 0x00000001124b9a28 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508 22 UIKit 0x0000000112498248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853 23 UIKit 0x00000001124ae8a9 -[UITableView layoutSubviews] + 210 24 UIKit
0x0000000112438a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536 25 QuartzCore 0x000000010fdfbec2 -[CALayer layoutSublayers] + 146 26 QuartzCore 0x000000010fdf06d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 27 QuartzCore 0x000000010fdf0546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 28 QuartzCore 0x000000010fd5c886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 29 QuartzCore 0x000000010fd5da3a _ZN2CA11Transaction6commitEv + 462 30 QuartzCore 0x000000010fd5e0eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 31 CoreFoundation 0x0000000111cb0ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23 32 CoreFoundation 0x0000000111cb0c00 __CFRunLoopDoObservers + 368 33 CoreFoundation 0x0000000111ca6a33 __CFRunLoopRun + 1123 34 CoreFoundation
0x0000000111ca6366 CFRunLoopRunSpecific + 470 35 GraphicsServices
0x0000000114a34a3e GSEventRunModal + 161 36 UIKit
0x00000001123b8900 UIApplicationMain + 1282 37 HawkExpress
0x000000010e4baa23 main + 99 38 libdyld.dylib
0x0000000114e5d145 start + 1) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

+3

我認爲你的問題的標題可能會更好;它將所有與Core Data有關的事情都做了,而與NSArray沒有多大關係。 – trojanfoe

+0

隨時更改它或讓我知道它應該是什麼,因爲我在問題中說,iOS不是我的領域,我不知道發生了什麼 – apmartin1991

+0

問題在這部分,請發佈代碼> >>> NSFetchRequest * request = [self requestFirstByAttribute:attribute withValue:searchValue inContext:context]; [request setPropertiesToFetch:[NSArray arrayWithObject:attribute]]; – hummingV

回答

1

您需要檢查NSArray的結果是零之前,你可以做一個計數。

if (array != nil) { 
    NSUInteger count = [array count]; // May be 0 if the object has been deleted. 
    // 
} 
else { 
    // Deal with error. 
} 

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

+0

你呢?從什麼時候Objective-C關心通過一個'nil'對象指針發送消息? – trojanfoe

+0

這個答案不會幫助,當我在這裏創建一個數組時失敗:NSArray * results = [self executeFetchRequest:request inContext:context];不是當我做一個計數 – apmartin1991

+0

您是否設置了請求實體? NSEntityDescription * entity = [NSEntityDescription entityForName:@「Event」inManagedObjectContext:context]; [request setEntity:entity]; –

0

試圖註釋掉該行[request setPropertiesToFetch:[NSArray arrayWithObject:attribute]];

「壞取得請求(NSManagedObjectResultType不propertiesToFetch的內容兼容)」手段,你似乎期望默認NSFetchRequestResultTypeNSManagedObjectResultType)。但是您請求不要獲取完整的實體,但只需要提供ONE屬性(爲什麼,順便說一下,您的方法提供了預期值,並且只提供了一個要獲取的屬性)。

我解決了這個:

- (id)findEntityNamed:(NSString*)entity withKeyPath:(NSString*)path equalTo:(id)value 
{ 
    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:entity 
            inManagedObjectContext:context]]; 

    NSPredicate* predicate = 
    [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:path] 
            rightExpression:[NSExpression expressionForConstantValue:value] 
              modifier:NSDirectPredicateModifier 
               type:NSEqualToPredicateOperatorType 
              options:0]; 

    [fetchRequest setPredicate:predicate]; 
    [fetchRequest setFetchLimit:1];   // ONLY 1 OBJECT IS FETCHED! 

    NSError* err = nil; 
    NSArray* results = [context executeFetchRequest:fetchRequest error:&err]; 
    [fetchRequest release]; 

    if(err) 
    { 
     // do logging stuff etc. 
    } 

    if([results count] < 1) 
     return nil; 

    return [results lastObject]; 
} 

你可以試試這個,並告訴我們,如果這更像是你一直在尋找

0

你NSFetchRequest對象可能無法正確構建。

這是一個有效的NSFetchRequest示例。通過google搜索「NSFetchRequest示例」可以找到更多。

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", aTitle]; //<----- (a) 
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:moc]]; 
[request setPredicate:predicate]; 

要小心,在(A),該字符串必須嚴格遵循謂詞格式字符串模式(見NSPrediate類的引用),名稱必須是個人實體的財產在你的管理對象模型

而問題在於這部分

NSFetchRequest *request = [self requestFirstByAttribute:attribute withValue:searchValue inContext:context]; 
[request setPropertiesToFetch:[NSArray arrayWithObject:attribute]]; 

因此,如果仍未解決,請發佈方法[self requestFirstByAttribute ...]的代碼。