我堆叠了这个错误:
CoreData:错误:严重的应用程序错误。抓住了一个例外 在调用期间来自NSFetchedResultsController的委托 -controllerDidChangeContent :.尝试从第0部分删除第0行,但在使用userInfo(null)
进行更新之前只有0部分
我不知道是谁调用删除操作 - 说实话,我在整个项目中没有任何删除操作。任何想法如何调试这种情况?
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch(type) {
case .insert:
self.operation.append(BlockOperation(block: {
self.tableView?.insertRows(at: [newIndexPath!], with: .bottom)
})
)
// self.tableView?.scrollToRow(at: newIndexPath, at: .bottom, animated: true)
break
case .update:
self.operation.append(BlockOperation(block: {
self.tableView?.reloadRows(at: [indexPath!], with: .bottom)
})
)
break
default:
break
}
}
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView?.beginUpdates()
for o in self.operation {
o.start()
}
self.tableView?.endUpdates()
}
提前致谢。
更新1
override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChannelTableCell = tableView.dequeueReusableCell(withIdentifier: "ChannelTableCell", for: indexPath) as! ChannelTableCell
cell.setChannel(self.channelController.object(at: indexPath), delegate: self)
return cell
}
更新2
我已经简化了didChange方法,现在在reloadRows()调用时得到错误。
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch(type) {
case .insert:
self.tableView?.insertRows(at: [newIndexPath!], with: .bottom)
break;
case .update:
self.tableView?.reloadRows(at: [indexPath!], with: .bottom)
break
default:
break
}
}
堆栈
*** First throw call stack:
(
0 CoreFoundation 0x0000000109db2d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001089cb21e objc_exception_throw + 48
2 CoreFoundation 0x0000000109db6e42 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010856066d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x000000010ab1ab60 -[UITableView _endCellAnimationsWithContext:] + 6265
5 UIKit 0x000000010ab34f62 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 331
6 Joker 0x00000001080335ac _TFC5Joker17AirViewController10controllerfTGCSo26NSFetchedResultsControllerPSo20NSFetchRequestResult__9didChangeP_2atGSqV10Foundation9IndexPath_3forOSC26NSFetchedResultsChangeType12newIndexPathGSqS4___T_ + 860
7 Joker 0x0000000108033740 _TToFC5Joker17AirViewController10controllerfTGCSo26NSFetchedResultsControllerPSo20NSFetchRequestResult__9didChangeP_2atGSqV10Foundation9IndexPath_3forOSC26NSFetchedResultsChangeType12newIndexPathGSqS4___T_ + 256
8 CoreData 0x0000000109a25400 __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke + 6608
9 CoreData 0x00000001098fa9a7 developerSubmittedBlockToNSManagedObjectContextPerform + 199
10 CoreData 0x00000001098fa85f -[NSManagedObjectContext performBlockAndWait:] + 255
11 CoreData 0x0000000109a23a17 -[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:] + 119
12 CoreFoundation 0x0000000109d505ec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
13 CoreFoundation 0x0000000109d504eb _CFXRegistrationPost + 427
14 CoreFoundation 0x0000000109d50252 ___CFXNotificationPost_block_invoke + 50
15 CoreFoundation 0x0000000109d13282 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
16 CoreFoundation 0x0000000109d1231b _CFXNotificationPost + 667
17 Foundation 0x000000010849181b -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
18 CoreData 0x00000001098e2d90 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 704
19 CoreData 0x0000000109979a91 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:] + 1713
20 CoreData 0x00000001098dcb32 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 3554
21 CoreData 0x00000001098b5764 -[NSManagedObjectContext executeFetchRequest:error:] + 420
22 libswiftCoreData.dylib 0x000000010c769e78 _TFE8CoreDataCSo22NSManagedObjectContext5fetchuRxSo20NSFetchRequestResultrfzGCSo14NSFetchRequestx_GSax_ + 56
...
)
答案 0 :(得分:1)
问题的直接原因是,当您使用indexPath
时,您正在使用newIndexPath
进行行更新。
原因是indexPath
是之前任何插入或删除的索引,而newIndexPath
是插入和删除之后的索引。
如果你想从fetchedResultsController更新tableView或collectionView并让它从不崩溃,你就是这样做的:
有5个可变对象 - rowInsert,rowDelete,rowUpdate,sectionInsert,sectionDelete。 rowInsert,rowDelete和rowUpdate是indexPaths的可变数组(即NSMutableArray<NSIndexPath*>*
)。 sectionInsert和sectionDelete是可变索引集(即NSMutableIndexSet*
)。在controllerWillChangeContent
中清除数组。在每次更改时添加适当的indexPath。对于插入和更新,请使用newIndexPath
,对于删除使用indexPath
,将add newIndexPath
移至rowInsert,将indexPath
移至rowDelete。在controllerDidChangeContent
排序rowInsert和sectionInsert升序和排序sectionDelete和rowDelete降序。 (您可以按行它的顺序保留rowUpdate。)现在按以下顺序应用更改:rowDelete,sectionDelete,sectionInsert,rowInsert,rowUpdate。