gpt4 book ai didi

iphone - Apple 的 iPhone SDK 3.0 CoreDataBooks 示例是否已损坏?

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

我在我的应用程序中遇到了这个问题,并使用 Apple 的 CoreDataBooks 重现了它。对代码进行 0 次修改的代码库。问题是,如果您对托管对象进行编辑,并保存上下文,结果是只有一行的部分现在没有行,并且创建了一个新部分,其中添加了结果对象,那么您会得到无效更新异常。它似乎也取决于行的顺序。

要重新创建,请按照以下步骤操作:

  1. 打开 CoreDataBooks 示例的新实例
  2. 编辑理查德·道金斯 (Richard Dawkins) 的书《自私的基因》(The Selfish Gene),并将作者更改为本 (Ben)。保存。如果您返回主视图,您会注意到这本书位于名为 Ben 的新部分下。
  3. 再次编辑这本书,并将作者更改为 Doug。保存。您将得到以下异常:

    2009-10-01 15:14:29.050 CoreDataBooks[10898:20b] *** Assertion failure in -[UITableView_endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-963.10/UITableView.m:729 2009-10-01 15:14:29.051 CoreDataBooks[10898:20b] Serious application error.  Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted). with userInfo (null)

我会发布我自己的应用程序中的代码,以确保我没有缺少任何必需的委托(delegate)方法,除非上面的示例显示这是一个直接影响 Apple 的 CoreDataBooks 示例的问题。 NSFetchedResultsController 委托(delegate)方法都是按照它们自己的示例实现的。他们的代码似乎也没有忘记处理新创建的部分,因为它在其他情况下可以使用不同的部分顺序。

如有任何帮助,我们将不胜感激。 CoreDataBooks 示例中的示例修改代码会很棒。

最佳答案

解决此问题的方法似乎是更改 RootViewController 的表更新部分中的这些行:

    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;

至:

         case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

由于某种原因。问题似乎来自于删除节中的最后一行;看来除非您添加额外的 insertRows,否则 UITableView 会因为已删除的部分突然获得一大堆额外的行而感到 panic 。

我花了几个小时才弄清楚这个问题......

关于iphone - Apple 的 iPhone SDK 3.0 CoreDataBooks 示例是否已损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1505926/

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