核心数据 EXC_BAD_ACCESS 保存失败?
我的核心数据模型无法保存。我可以向其中插入一种对象(图表 - 见下文),然后保存即可。我可以添加此类对象的多个副本,然后保存就可以了。当我插入不同的对象时,任何一个都会失败,通常是这样的:
#2 0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()
其他对象并不复杂:只是字符串和数字值。它们的值是在保存时设置的。我已将程序简化为 NSPersistentDocument 的初始化:以下是在我的 NSPersistentDocument 子类中的 initWithType:error:
中调用的相关代码:
...
// Disable undo registration before inserting default objects
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// Insert the root graph
self.root = [NSEntityDescription insertNewObjectForEntityForName:@"Graph" inManagedObjectContext:[self managedObjectContext]];
self.root.createdDate = [NSDate date];
self.root.lastModifiedDate = [NSDate date];
self.root.name = @"Untitled Model";
self.root.colour = [NSColor colorWithDeviceRed:0.6 green:0.6 blue:0.6 alpha:1.0];
// Insert default drawing parameters
// fails when including this line
self.drawing = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:[self managedObjectContext]];
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
...
运行时,我得到:
2011-07-23 07:46:58.012 Z2[15774:a0f] Managed Object Context: <NSManagedObjectContext: 0x2000c65e0>
2011-07-23 07:46:58.013 Z2[15774:a0f] Persistent Store Coordinator: <NSPersistentStoreCoordinator: 0x200063700>
2011-07-23 07:46:58.013 Z2[15774:a0f] Entity: Graph
...<full Entity list>
2011-07-23 07:46:58.016 Z2[15774:a0f] Entity: Drawing
2011-07-23 07:46:58.018 Z2[15774:a0f] Object: <Graph: 0x20004e220> (entity: Graph; id: 0x20003e400 <x-coredata:///Graph/t527187E0-DC67-4A9E-BA88-67A4CD3445222> ; data: {
colour = "(...not nil..)";
createdDate = "2011-07-22 21:46:54 +0000";
errorString = nil;
firstDate = nil;
lastDate = nil;
lastModifiedDate = "2011-07-22 21:46:54 +0000";
lines = ();
name = "Untitled Model";
nodes = ();
profiles = ();
test = 0;
warningString = nil;
})
2011-07-23 07:46:58.019 Z2[15774:a0f] Object: <Drawing: 0x20004f140> (entity: Drawing; id: 0x200062280 <x-coredata:///Drawing/t527187E0-DC67-4A9E-BA88-67A4CD3445223> ; data: {
centrex = 0;
centrey = 0;
textSize = 12;
typeface = "Helvetica Neue";
zoomFactor = 1;
})
Program received signal: “EXC_BAD_ACCESS”.
每个实体都有自己的类NSManagedObject 的子类。每个属性都被定义为动态属性。每个属性都有一个默认值,在合理的情况下使用它们。
我看到其他人建议对此类问题进行过度释放,但我不在核心数据代码中的任何地方执行保留和释放。我选择在 Graph 对象之后插入哪个实体并不重要;他们都失败了。
核心数据工作是由框架(Graph)完成的。该框架是从基于文档的应用程序 (Z) 调用的,该应用程序是该框架的文档创建对象的子类。
这是保存面板末尾的堆栈跟踪:
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb) bt
#0 0x00007fff80352480 in CFDictionarySetValue ()
#1 0x00007fff86c0b2ed in _PFCMT_SetValue ()
#2 0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()
#3 0x00007fff86c44b2b in -[NSSQLCore commitChanges:] ()
#4 0x00007fff86c3926e in -[NSSQLCore saveChanges:] ()
#5 0x00007fff86bfcc8b in -[NSSQLCore executeRequest:withContext:] ()
#6 0x00007fff86bfc051 in -[NSPersistentStoreCoordinator(_NSInternalMethods) executeRequest:withContext:] ()
#7 0x00007fff86c30123 in -[NSManagedObjectContext save:] ()
#8 0x00007fff8144179d in -[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] ()
#9 0x0000000100020460 in -[GraphDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/GraphDocument.m:380
#10 0x00000001000010bc in -[ZDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/ZDocument.m:110
#11 0x00007fff81316bd2 in -[NSDocument _writeSafelyToURL:ofType:forSaveOperation:error:] ()
#12 0x00007fff81315fd0 in -[NSDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#13 0x00007fff814406dc in -[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#14 0x00007fff81311356 in -[NSDocument saveToURL:ofType:forSaveOperation:error:] ()
#15 0x00007fff81314404 in -[NSDocument _saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#16 0x00007fff81311637 in -[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#17 0x00007fff8131377c in -[NSDocument _savePanelWasPresented:withResult:inContext:] ()
#18 0x00007fff8148335e in -[NSSavePanel _didEndSheet:returnCode:contextInfo:] ()
#19 0x00007fff81227d45 in -[NSApplication endSheet:returnCode:] ()
My Core Data model just won't save. I can insert one kind of object (Graph - see below) to it, and saving will work. I can add multiple copies of that kind of object and saving will work. When I insert a different object, any one, it fails, usually with this:
#2 0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()
The other objects are not complicated: just String and Number values. Their values are set at save time. I have reduced the program to just the initialisation of the NSPersistentDocument: the following is the relevant code called in initWithType:error:
in my NSPersistentDocument subclass:
...
// Disable undo registration before inserting default objects
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// Insert the root graph
self.root = [NSEntityDescription insertNewObjectForEntityForName:@"Graph" inManagedObjectContext:[self managedObjectContext]];
self.root.createdDate = [NSDate date];
self.root.lastModifiedDate = [NSDate date];
self.root.name = @"Untitled Model";
self.root.colour = [NSColor colorWithDeviceRed:0.6 green:0.6 blue:0.6 alpha:1.0];
// Insert default drawing parameters
// fails when including this line
self.drawing = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:[self managedObjectContext]];
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
...
When run, I get:
2011-07-23 07:46:58.012 Z2[15774:a0f] Managed Object Context: <NSManagedObjectContext: 0x2000c65e0>
2011-07-23 07:46:58.013 Z2[15774:a0f] Persistent Store Coordinator: <NSPersistentStoreCoordinator: 0x200063700>
2011-07-23 07:46:58.013 Z2[15774:a0f] Entity: Graph
...<full Entity list>
2011-07-23 07:46:58.016 Z2[15774:a0f] Entity: Drawing
2011-07-23 07:46:58.018 Z2[15774:a0f] Object: <Graph: 0x20004e220> (entity: Graph; id: 0x20003e400 <x-coredata:///Graph/t527187E0-DC67-4A9E-BA88-67A4CD3445222> ; data: {
colour = "(...not nil..)";
createdDate = "2011-07-22 21:46:54 +0000";
errorString = nil;
firstDate = nil;
lastDate = nil;
lastModifiedDate = "2011-07-22 21:46:54 +0000";
lines = ();
name = "Untitled Model";
nodes = ();
profiles = ();
test = 0;
warningString = nil;
})
2011-07-23 07:46:58.019 Z2[15774:a0f] Object: <Drawing: 0x20004f140> (entity: Drawing; id: 0x200062280 <x-coredata:///Drawing/t527187E0-DC67-4A9E-BA88-67A4CD3445223> ; data: {
centrex = 0;
centrey = 0;
textSize = 12;
typeface = "Helvetica Neue";
zoomFactor = 1;
})
Program received signal: “EXC_BAD_ACCESS”.
Every Entity has its own class that subclasses NSManagedObject. Every attribute is defined as a dynamic property. Every attribute has a default value, where it's reasonable to have them.
I have seen others suggest over-releasing for this type of problem, but I do not perform retain and release anywhere in the Core Data code. It doesn't matter which Entity I choose to insert after the Graph object; they all fail.
The Core Data work is done by a framework (Graph). The framework is called from a document-based application (Z) that subclasses the framework's document-creating object.
Here's the stack trace from the end of the save panel:
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb) bt
#0 0x00007fff80352480 in CFDictionarySetValue ()
#1 0x00007fff86c0b2ed in _PFCMT_SetValue ()
#2 0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()
#3 0x00007fff86c44b2b in -[NSSQLCore commitChanges:] ()
#4 0x00007fff86c3926e in -[NSSQLCore saveChanges:] ()
#5 0x00007fff86bfcc8b in -[NSSQLCore executeRequest:withContext:] ()
#6 0x00007fff86bfc051 in -[NSPersistentStoreCoordinator(_NSInternalMethods) executeRequest:withContext:] ()
#7 0x00007fff86c30123 in -[NSManagedObjectContext save:] ()
#8 0x00007fff8144179d in -[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] ()
#9 0x0000000100020460 in -[GraphDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/GraphDocument.m:380
#10 0x00000001000010bc in -[ZDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/ZDocument.m:110
#11 0x00007fff81316bd2 in -[NSDocument _writeSafelyToURL:ofType:forSaveOperation:error:] ()
#12 0x00007fff81315fd0 in -[NSDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#13 0x00007fff814406dc in -[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#14 0x00007fff81311356 in -[NSDocument saveToURL:ofType:forSaveOperation:error:] ()
#15 0x00007fff81314404 in -[NSDocument _saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#16 0x00007fff81311637 in -[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#17 0x00007fff8131377c in -[NSDocument _savePanelWasPresented:withResult:inContext:] ()
#18 0x00007fff8148335e in -[NSSavePanel _didEndSheet:returnCode:contextInfo:] ()
#19 0x00007fff81227d45 in -[NSApplication endSheet:returnCode:] ()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于它在更改对象 ID 时失败,我猜测问题出在关系上。问题很可能在于所需的关系、删除规则或验证。
Since it fails on the change of object IDs, I'm going to guess the problem is with a relationship. Most likely the problem is with a required relationship, a delete rule or a validation.