【发布时间】:2013-05-26 17:31:41
【问题描述】:
我有每个都有一个类别的项目。我使用NSFetchedResultsController 来显示项目并按类别名称将它们分组。
self.fetchedResultsController = [Item MR_fetchAllSortedBy:@"category.categoryName" ascending:YES withPredicate:nil groupBy:@"category.categoryName" delegate:self];
但是当我添加如下两个新项目时,只有一个具有类别的项目被插入到列表中。如果我重新启动应用程序,则没有类别名称的应用程序将按原样显示在没有部分的顶部。
这就是我插入的方式(使用 Magical Record):
Category *category = [Category MR_createEntity];
category.categoryName = @"My Category";
Item *itemWithCategory = [Item MR_createEntity];
itemWithCategory.itemName = @"Item with category";
itemWithCategory.category = category;
Item *itemWithoutCategory = [Item MR_createEntity];
itemWithoutCategory.itemName = @"Item without category";
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
当sectionNameKeyPath 引用子对象上的属性并且该子对象为nil 时,为什么NSFetchedResultsController 在添加新项目时不会收到通知?
【问题讨论】:
-
这不仅发生在节名键路径引用相关对象时,而且当键位于获取的对象本身上时。例如,如果您将它们放在 Item.name 上,然后添加一个有名称和一个没有名称,您将看到相同的效果。但是,在这种情况下,您可以始终使用 "" 而不是 nil 来规避问题。
标签: ios objective-c cocoa-touch core-data nsfetchedresultscontroller