我在我的應用程序中使用核心數據,它將存儲下載的數據。當應用程序下載數據時,我將它們保存在privateQueueContext(NSPrivateQueueConcurrencyType)- [UICollectionView _endItemAnimations]崩潰時的聲明失敗
一旦核心數據保存完成,我應該更新UI。爲了做到這一點,我定義了NSFetchedResultsController委託方法。正如我預料的那些委託方法在覈心數據發生變化時被調用。
在didChangeObject
方法,我將新電池,但它會導致崩潰時,有插入的核心數據許多對象並給出以下錯誤消息
*** Assertion failure in -[UICollectionView _endItemAnimations] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (379) must be equal to the number of items contained in that section before the update (325), plus or minus the number of items inserted or deleted from that section (52 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
我假設完成新的單元格中插入動畫之前數據源的變化。
我希望很多人會遇到同樣的問題,堆棧溢出中有這麼多類似的問題,但沒有一個答案對我來說是正確的。你能幫我解決這個問題嗎?
插入在覈心數據的新對象
for (NSDictionary *obj in books){
Book *book = [Book insertInManagedObjectContext:context];
book.udid = [obj objectForKey:@「id」]
}
[context performBlock:^{
NSError *error = nil;
if ([context save:&error]) {
NSLog(@「success」);
}
}];
讀取結果控制器委託
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
self.blockOperation = [NSBlockOperation new];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
__weak UICollectionView *collectionView = self.collectionView;
switch(type) {
case NSFetchedResultsChangeInsert:{
[self.blockOperation addExecutionBlock:^{
[collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:newIndexPath.item + 1 inSection:newIndexPath.section]]];
}];
break;
}
default:
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.collectionView performBatchUpdates:^{
[self.blockOperation start];
} completion:nil];
}
您能否提供完整的錯誤文本?通常在你包含的行後面有額外的信息。 – jrturton
@jrturton感謝您的回覆***聲明失敗 - [UICollectionView _endItemAnimations],/ SourceCache /UIKit_Sim/UIKit-3318.16.14/UICollectionView.m:3917 – thavasidurai
CoreData:錯誤:嚴重的應用程序錯誤。在調用-controllerDidChangeContent:期間,NSFetchedResultsController的委託捕獲到異常。無效的更新:部分0中的項目數量無效。更新後(379)的現有部分中包含的項目數量必須等於更新前(325)的該部分中包含的項目數量,加上或減去數字插入或從該部分刪除的項目(插入了52個,刪除了0個),加上或減去移入或移出該部分的項目數(移入0個,移出0個)。與userInfo(null) – thavasidurai