自定义托管对象间歇性地挂在关系访问器中
在 iOS 上(使用 Xcode 4、iPad Simulator 4.3),我有一个“文章”实体和一个“作者”实体。有两种关系连接这两者(一篇文章有一个“作者”关系[可选,对多],一个作者也有一个“文章”关系[可选,对多])。
如果我从一个空数据库开始(从头开始,从模拟器中删除应用程序并再次点击调试)。它工作正常,我可以毫无问题地向数据库添加数百个项目。
但是,如果我首先向数据库添加一些项目,然后停止应用程序(是否按主页按钮)并再次启动(再次点击调试按钮),数据就在那里,我可以很好地“读取”数据,并且可以添加数据对其他实体罚款。但是,当我添加新作者(而不是文章)时,有时新闻会停在我的“Author.m”类(它是 NSManagedObject 的子类)中该函数的第二行:
- (void)addArticlesObject:(Article *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"articles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"articles"] addObject:value];
[self didChangeValueForKey:@"articles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
没有任何消息在控制台(调试器输出)中,如果我向前迈出一步,我将被发送到 __CFTypeCollectionRetain ,然后得到 EXC_BADACCESS 。
最后一步并不总是相同的,有时它不会返回,CPU 会达到 100%(50% 实际上它是双核机器),直到我停止或终止“应用程序”进程(xcode 响应良好)。
有什么想法吗?
On iOS (using Xcode 4, iPad Simulator 4.3) I have an "Article" entity and an "Author" entity. There are two relationships connecting these two (an article has an "authors" relationship [optional, to-many] and an author also has an "articles" [optional, to-many]).
If I start from an empty database (starting from scratch, removing the app from the simulator and hitting debug again). It works fine, I can add like hundreds of items to the database without a problem.
BUT if I add some items to the database first and then stop the app (press home button or not) and start again (hit the debug button again) the data is there, I can "read" the data fine and I can add data to other entities fine. But when I add new authors (and not articles), sometimes the press stops on the second line of this function in my "Author.m" class (which is a subclass of NSManagedObject):
- (void)addArticlesObject:(Article *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"articles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"articles"] addObject:value];
[self didChangeValueForKey:@"articles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
WITHOUT any messages in the console (debuger output) and if I step forward I would be sent to __CFTypeCollectionRetain
and then get a EXC_BADACCESS .
This final step is not always the same, some times it just wont return and CPU goes to 100% (50% actually it's a dual core machine) until I hit stop or kill the "app's" process (xcode responds fine).
Any ideas what is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会看到由双边对多关系的副作用引起的无限循环,例如,当您将
Article
对象添加到Author
对象时,会导致>Article
对象添加另一个Author
实例,该实例又添加另一个Article
等等...这不会立即产生任何错误,因为很长的循环通常是代码的一部分。您收到的错误表明您在保留集合(数组、字典、集合)对象的 Core Foundation 对象中遇到某种溢出情况。
检查所有自定义访问器是否有副作用,以确保没有触发循环。您可以将日志语句放入访问器中以查看它们何时被调用。这会让你看到循环。
You are probably looking at an infinite loop caused by side effects of the two sided to-many relationship e.g. when you add an
Article
object to aAuthor
object that causes theArticle
object to add anotherAuthor
instance which in turn adds anotherArticle
and so on...This would not produce any immediate errors because very long loops are often part of the code. The error you are getting suggest that you encounter some kind of overflow situation in a Core Foundation object that retains collection (arrays, dictionaries, sets) objects.
Check all your custom accessors for side effects to make sure you are not triggering a loop. You can put log statements inside the accessors to see when they are being called. That will let you see the loop.
问题似乎是自定义托管对象类与数据模型不同步造成的。显然,我更改了关系的多对多或可选属性,并且没有重新生成自定义类。现在问题消失了。
It seems the problem was a result of custom managed object classes not being in sync with the data model. Apparently I changed a relation's to-many or optional property and did not regenerate the custom classes. Now the problem is gone.