我的核心数据模型不会保存。我可以插入一种对象(图表 - 见下文),保存将起作用。我可以添加这种对象的多个副本,保存将起作用。当我插入一个不同的对象时,它会失败,通常是这样的:
#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的子类。每个属性都定义为动态属性。每个属性都有一个默认值,这是合理的。
我见过其他人建议过度发布此类问题,但我不会在Core Data代码中的任何位置执行保留和释放。我选择在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:] ()
答案 0 :(得分:0)
由于对象ID的更改失败,我猜测问题与关系有关。最有可能的问题是需要的关系,删除规则或验证。