gpt4 book ai didi

iphone - NSFetchedResultsController 返回索引路径为空的对象

转载 作者:行者123 更新时间:2023-12-03 16:16:31 24 4
gpt4 key购买 nike

详情见评论。

以下代码:

// Perform the fetch...
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

// Confirm that objects were fetched by counting them...
NSLog(@"Number of Objects = %i",
[[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist by counting them...
NSLog(@"Numbers of Sections = %i",
[[fetchedResultsController sections] count]);

for (id section in [fetchedResultsController sections]) {
// Count number of objects in each section
// _The fact that this outputs 0 is the first sign of trouble_
NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (Reminder *reminder in [fetchedResultsController fetchedObjects]) {
// Confirm that the objects fetched are in fact real objects
// by accessing their "textContent" property...
NSLog(@"textContent=%@", reminder.textContent);

// Show that the fetched objects are being returned
// with a (null) indexPath...
// _The second sign of trouble..._
NSLog(@"IndexPath=%@",
[fetchedResultsController indexPathForObject:reminder]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr
length:2];

// _Application crashes on this line because the fetched
// objects do not have indexPaths_
Reminder *testReminder = (Reminder *)[fetchedResultsController
objectAtIndexPath:indexPath];
NSLog(@"textContent = %@", testReminder.textContent);

产生以下输出:

2010-07-17 00:48:41.865 Reminders[27335:207] Number of Objects = 3
2010-07-17 00:48:41.867 Reminders[27335:207] Numbers of Sections = 1
2010-07-17 00:48:41.868 Reminders[27335:207] Number of Objects in Section = 0
2010-07-17 00:48:41.870 Reminders[27335:207] textContent=Imported Object 3
2010-07-17 00:48:41.871 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.873 Reminders[27335:207] textContent=Imported Object 2
2010-07-17 00:48:41.873 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.874 Reminders[27335:207] textContent=Imported Object 1
2010-07-17 00:48:41.875 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.887 Reminders[27335:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

任何想法将不胜感激。仅供引用,如果我使用不同的模板作为起点,上面的代码可以在单独的应用程序中完美运行。 IE。如果我使用“基于窗口的应用程序”模板,代码将失败。如果我使用“基于导航的应用程序”,代码将按预期工作。

更新:TechZen 想知道问题是否是由我的提醒实体引起的。我认为这是一个值得研究的好主意,因此我执行了以下操作:

  1. 创建两个默认模板应用程序:“基于窗口的应用程序”和“基于导航的应用程序”(均启用了核心数据)

  2. 将执行上述测试所需的最少代码从基于导航的系统复制到基于窗口的系统(几乎只是“xcdatamodel”文件、fetchedresultscontroller 以及添加测试对象的方法)。

上述代码在新的“Reminder-entity free”基于窗口的应用程序中仍然失败。 (在这个新的测试应用程序中,实际上我自己编写的代码为零(测试代码之外),这只是剪切并粘贴在一起的模板代码。)

所以现在,我正在寻找任何方法在创建“基于窗口的应用程序”后运行上述代码。以下是使用基于导航的默认实体执行测试的代码,以防有人有兴趣尝试一下:

更新 请注意,正如 TechZen 下面指出的那样,无论使用空数据库运行,此代码都会崩溃,因此,如果从基于窗口的应用程序开始,请首先向数据库添加一些对象,然后添加测试代码。

// Confirm that objects were fetched
NSLog(@"Number of Objects = %i",
[[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist
NSLog(@"Numbers of Sections = %i",
[[fetchedResultsController sections] count]);

for (id section in [fetchedResultsController sections]) {

// Count number of objects in sections
// _The fact that this outputs 0 is the first sign of trouble_
NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (NSManagedObject *managedObject in [fetchedResultsController fetchedObjects]) {

// Confirm that the objects fetched are in fact real objects,
// by accessing their "timeStamp" property
NSLog(@"TimeStamp=%@", [[managedObject valueForKey:@"timeStamp"] description]);

// Show that the fetched objects are being returned
// with a (null) indexPath
// _The second sign of trouble..._
NSLog(@"IndexPath=%@",
[fetchedResultsController indexPathForObject:managedObject]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr
length:2];

// _Application crashes on this line, because the fetched
// objects do not have indexPaths_
NSManagedObject *managedObject = [fetchedResultsController
objectAtIndexPath:indexPath];
NSLog(@"textContent = %@", [[managedObject valueForKey:@"timeStamp"] description]);

更新这是使用新的剪切粘贴代码时的输出

2010-07-18 15:33:41.264 Reminders[30898:207] Number of Objects = 3
2010-07-18 15:33:41.266 Reminders[30898:207] Numbers of Sections = 1
2010-07-18 15:33:41.267 Reminders[30898:207] Number of Objects in Section = 0
2010-07-18 15:33:41.270 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.271 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.272 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.273 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.274 Reminders[30898:207] TimeStamp=2010-07-18 13:58:59 -0400
2010-07-18 15:33:41.275 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.276 Reminders[30898:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

更新所以我将这个问题缩小到与 SDK 版本相关的问题,我现在有一个项目,如果我使用 Simulator 3.2 构建它会崩溃,而使用 Simulator 3.1.3 构建它可以工作美好的。但是如果我添加 UITableViewController,然后使用 Simulator 3.2 进行构建,那么它会再次正常工作。所以我创建了一个new stackoverflow发帖提出问题:如果您使用 NSFetchedResultsController 而不使用 UITableViewController,您如何与对象交互? (因为 IndexPaths 不可靠)。

更新此问题(暂时)通过使用 -[NSFetchedResultsController fetchedObjects] objectAtIndex:] 访问对象来解决。

最佳答案

您是否尝试过在仅内存跟踪模式下使用NSFetchedResultsController? (仅内存跟踪:委托(delegate)非nil且文件缓存名称设置为nil)

关于iphone - NSFetchedResultsController 返回索引路径为空的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3270328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com