我尝试将非某些Core Data对象与Core Data对象混合使用,我使用TLIndexPathTools这样做。
- (TLIndexPathDataModel *)controller:(TLIndexPathController *)controller willUpdateDataModel:(TLIndexPathDataModel *)oldDataModel withDataModel:(TLIndexPathDataModel *)updatedDataModel
{
NSMutableArray *sectionInfos = [NSMutableArray array];
TLIndexPathSectionInfo *section0 = [[TLIndexPathSectionInfo alloc] initWithItems:@[@"item1", @"item2"] name:@"section0"];
[sectionInfos addObject:section0];
[sectionInfos addObjectsFromArray:updatedDataModel.sections];
return [[TLIndexPathDataModel alloc] initWithSectionInfos:sectionInfos identifierKeyPath:nil];
}
我正在使用TLIndexPathTableViewController并设置TLIndexPathController,如下所示:
- (void)setupIndexPathController
{
NSFetchRequest *fetchRequest;
NSString *sectionNameKeyPath;
if (self.product) {
fetchRequest = [SubMenus getSubMenusForProduct:self.product];
sectionNameKeyPath = @"group.name";
}else if (self.cartProduct) {
fetchRequest = [SubMenuProductCart getSubMenusForProductCart:self.cartProduct];
sectionNameKeyPath = @"group.subMenusGroup.name";
}
[TLIndexPathController deleteCacheWithName:@"SubMenuGroupTitles"];
self.indexPathController = nil;
self.indexPathController = [[TLIndexPathController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:sectionNameKeyPath identifierKeyPath:nil cacheName:@"SubMenuGroupTitles"];
NSError *error;
[self.indexPathController performFetch:&error];
}
在我的viewDidAppear中调用此设置方法,以便获取和刷新对象。
但是当我调用[self.indexPathController.dataModel itemAtIndexPath:indexPath]时;在我的DidSelectRow方法中,我得到了以下崩溃:
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array with userInfo
答案 0 :(得分:0)
崩溃似乎是您访问空数组。因此,请始终检查数组中的nil并计算大于零的条件,以确保不会发生崩溃。并且在将对象添加到数组之前进行检查,您是否已经初始化了数组。否则它永远不会添加任何对象