iPhone:核心数据:NSManagedObjects 和 UITableView 崩溃

发布于 2024-12-08 15:44:26 字数 1379 浏览 0 评论 0原文

这个 fetch 方法工作完美,NSLog 打印出数据库的内容...(NSArray 中的 fetchedObjects):

NSError *error2;
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error2];

if (!fetchedObjects) {
    NSLog(@"Error %@",[error2 localizedDescription]);
}

for (NSManagedObject *info in fetchedObjects) {

    NSLog(@"Name: %@", [info valueForKey:@"Name"]);
    NSLog(@"Description: %@", [info valueForKey:@"Description"]);
    NSLog(@"Ingredients: %@", [info valueForKey:@"Ingredients"]);

..但是当我尝试填充 tableView 时(我已经构建了很多表,并且感觉我很好地理解了如何他们工作),程序崩溃并显示“EXE_BAD_ACCESS at int retVal = UIApplicationMain(argc, argv, nil, nil);”。就是这样,没有其他错误消息。

为了尝试调试,我将这个简单的 NSLog 放入“cellForRowAtIndexPath”方法中,但它在 NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]) 处崩溃。

据我所知,我还没有发布该数组。

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
    NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
    NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
    cell.textLabel.text = [info3 valueForKey:@"Name"];

This fetch method work perfectly and the NSLog prints out the contents of the database...(fetchedObjects in an NSArray):

NSError *error2;
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error2];

if (!fetchedObjects) {
    NSLog(@"Error %@",[error2 localizedDescription]);
}

for (NSManagedObject *info in fetchedObjects) {

    NSLog(@"Name: %@", [info valueForKey:@"Name"]);
    NSLog(@"Description: %@", [info valueForKey:@"Description"]);
    NSLog(@"Ingredients: %@", [info valueForKey:@"Ingredients"]);

.. but when I try to populate a tableView (I have built many tables and feel i have a good understanding of how they work), the program crashes with a "EXE_BAD_ACCESS at int retVal = UIApplicationMain(argc, argv, nil, nil);." That is it, no other error messages.

To try and debug, I put this simple NSLog in the "cellForRowAtIndexPath" method, but it crashes at NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]).

I haven't released the array as far as I can tell.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
    NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
    NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
    cell.textLabel.text = [info3 valueForKey:@"Name"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-12-15 15:44:27

看起来您的 fetchedObjects 实例变量由于自动释放池刷新而被释放。

NSManagedObjectContext-executeFetchRequest:error: 方法返回一个 autoreleased NSArray 实例,您没有获得其所有权,因此,当执行到达 -tableView:cellForRowAtIndexPath: 方法时,自动释放池已被刷新,并且该对象被释放,现在您有一个悬空指针。啊,经典。

当然,解决方案是通过将 -executeFetchRequest:error: 发送到您的 来将 -retain 消息发送到返回的 NSArray NSManagedObjectContext

不要忘记在 -dealloc 方法中向其发送 -release 消息。

Looks like your fetchedObjects instance variable is being deallocated because of an autorelease pool flush.

NSManagedObjectContext's -executeFetchRequest:error: method returns an autoreleased NSArray instance, which you aren't obtaining ownership of, therefore by the time execution reaches the -tableView:cellForRowAtIndexPath: method, the autorelease pool has been flushed, and that object is deallocated, and you now have a dangling pointer. Ah, classic.

The solution, of course, is to send the -retain message to the returned NSArray from sending -executeFetchRequest:error: to your NSManagedObjectContext.

Don't forget to send the -release message to it in your -dealloc method.

泅人 2024-12-15 15:44:27

问题似乎出在这一行

NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];

尝试使用

NSManagedObject *info3 = [fetchedObjects objectAtIndex:0];

因为您的 UiTableViewCell 计数和 fetchedObjects 计数在整个过程中应该相等。

如果不匹配,则会出现索引错误。

The problem seems to be on this line

NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];

Try to use

NSManagedObject *info3 = [fetchedObjects objectAtIndex:0];

Because your UiTableViewCell count and fetchedObjects count should be equal at whole process.

If it doesn't match then it will give you index error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文