- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我目前遇到一个问题,当 FRC 的 fetchRequest 上的 fetchLimit 为 4 时,使用 NSFetchedResultsController 的 UITableViewController/UITableView 显示大约 86 个项目。我知道 86 个项目满足提取本身,我知道原因它们出现是因为 didChangeObject: atIndexPath... 被 86 中的每一个调用,我按默认实现的方式插入。
我的问题是为什么 fetchLimit 不限制 NSFetchedResultsController 尝试“更改”(在本例中插入)的对象数量?
我的应用程序用例是第一个选项卡显示我在应用程序启动时(在副线程上)获得的典型提要项目。我将它们保存到单独上下文中的 CoreData,最终合并到主线程的上下文中,并针对更改的内容启动 FRC 回调。我的问题专门针对不存在任何项目的初始情况。
这是我的 fetchRequest:
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:ENTITY_CONTENT_ITEM inManagedObjectContext:managedObjectContext];
// Set a limit on the number of items returned
[fetchRequest setFetchLimit:4];
// Set a Predicate to limit the fetch to featured items only
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"featured == YES AND contentType == %d", contentType]];
// Set the sort descriptors
NSSortDescriptor *sortDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortDate" ascending:NO] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDateDescriptor]];
上面的 contentType 只是一种区分应该在此选项卡和其他选项卡中显示的内容的方法。 Featured 是项目的 bool 属性,更像是用于显示目的的开关。
这是我的 didChangeObject:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[tableView cellForRowAtIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
我知道这个问题很难回答,但即使解释 FRC 如何决定调用 didChangeObject 的次数也会很有帮助。
最佳答案
https://devforums.apple.com/thread/60319?tstart=0
对于那些可以访问它的人,请检查一下。 Apple 开发人员对 iOS4.0 中 CoreData/NSFetchedResultsController/UITableViewController 堆栈的更改进行了很好、快速的总结。
There were changes made in 3.2 and 4.0 to improve the caching and performance as well as fix known issues. The persistent caching is much more aggressive, and so people misusing the cacheName have run into trouble. Setting the cacheName to nil instead, or cleaning up usage and calling +deleteCacheWithName when appropriate are solutions to that. The section recalculation was improved to perform more computation via the database instead of in-memory whenever possible. Sectioning triggers whenever the cache is rebuilt (or if not using a cache, when performFetch is called). The .description workaround makes the keypath refer to an unmodeled property (the -description method) which cause the section calculation to work in memory instead since the db doesn't know anything about -description.
In 4.0, there were also changes to UITableView to fix a number of issues involving the UITableViewController delegate callbacks. People who needed to use -reloadData on earlier iOS releases to work around trouble should be able to use the finer grained callbacks on iOS4.
-BenT (Apple)
鉴于此,我在 fetchedResultsController initWithFetchRequest 调用中将 .description 添加到我的 sectionNameKeyPath 中。这显然使部分计算发生在内存中而不是磁盘上,这解决了我的部分认为它们拥有的项目比实际更多的问题。
这适用于 4.0,而在 3.0 中,我只是从 controllerDidChangeContent 回调中调用 reloadData。如果您有类似问题,请随时给我留言。
关于iphone - objectAtIndex :0] numberOfObjects] > fetchLimit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302182/
救命!我已经为这个问题苦恼了 1 个小时了。 我正在我的 .h 文件中创建一个数组 @interface LeftMenuViewController : UIViewController
我想使用 NSMutableArray 中的 ValueForKey 替换 objectAtIndex。我已经尝试了下面的代码,但是没有用,它崩溃了, [[students valueForKey:@
我有一个包含 10 个对象的 Array。我将第一个对象放入带有 String 的 Label 中。 然后我想要一个将 objectAtIndex 增加 1 的方法。 这是我的代码: //.m @in
我有 NSMutableArray “Tag”,其对象位于索引 0 上:“data”、“data1”、“data2”,索引 1:“data3”,索引 2:“data2” ...我选择的标签是“data
嗨,我有一个数组数组,我在添加某个索引后删除了一个对象。不知何故,我认为这表现得很奇怪,或者我可能忽略了一些事情。这是代码。我用 nslogs 对其进行跟踪。 for (int i = 0;
在我的 UIScrollView 中,我的数组中有 30 张图像。我尝试在数组中获取 objectAtIndex。我希望能够在 UIScrollView 停止滚动时在特定图像中显示动画。所以我在下面尝
如何在我的方法中获取 objectAtIndex。 例如,如果我使用 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 我可以得到它 NSLog(@"
我正在尝试掌握 Objective C 和 Cocoa,所以我可能在这里使用了错误的术语。 我为一副纸牌创建了一个 Objective C 类,由我的主 AppDelegate.h 和 AppDele
我在使用 NSFetchedResultsController objectAtIndex 时出现内存泄漏:栈帧如下: 0 CoreFoundation __CFDataAllocate
我正在开发iOS应用程序。值存储在Array上,但是当我运行应用程序时,它崩溃,数组索引超出范围。请先帮助并谢谢。 代码: daysOfWeek = [NSArray arrayWithObjects
我有一个我想要一个值的 NSArray。 _membersArray = [_detailItem valueForKey:@"members"]; 当我使用此代码时: NSLog(@"%lu", (
我有一个 UITableView,它由从 XML 提要解析的数据数组填充。我正在努力寻找此错误的原因,并想知道是否有人可以帮助我。该错误不会经常发生。它仅在数组数量很大时发生,例如 10-15 个对象
我收到上述错误,但不知道为什么,因为我在另一个示例中实现了相同的代码。 “服务”是我的 NSMutableArray,“服务名称”是带有标签 2 的 TableView 上的标签。在 viewdidl
初学者 iOS 开发问题在这里。 我有以下格式的 plist: 我在我的应用程序中使用以下方法将这个 plist 转换为一个数组: NSMutableArray *array2 = [[NSMutab
我正在构建一个简单的 Swift CoreData TableView 应用程序。我已成功将数据保存到数据库并在 TableView 中重新加载数据。接下来我要做的是编辑这些数据。通过点击表格单元格/
所以我在这里有函数 collectionView: cellForItemAtIndexPath: func collectionView(collectionView: UICollectionVi
NSInteger *count = [monLessonArrayA count]; 对于 (i = 0; i < count; i++) { arrayVar = [monLessonArrayA
2012-06-15 17:53:25.532 BadgerNew[3090:707] *** Terminating app due to uncaught exception 'NSRangeEx
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: Is there some literal dictionary or array syntax in Obj
我有一个 NSArray,其中包含索引 0 - 9 的 10 个对象。数组中的每个条目都是一个引号。 当我的用户选择“随机引用”选项时,我希望能够从数组中选择一个随机条目并显示该条目中包含的文本。 有
我是一名优秀的程序员,十分优秀!