我有这个使用coredata
. 它有一个实体叫做OfferedClasses
,一个叫做Pupils
,一个叫做Professors
。
该classes
实体与其他每个实体都有关系。它可以有一个professor
和几个pupils
。
每个实体都有一个扩展类的类表示NSManagedObject
。
我也有两个窗口。一个由实体拥有AppDelegate
并拥有一个实体列表,其中classes
包含创建选项和删除classes
行的选项。
第二个窗口由我调用的一个类拥有PeopleManager
。我在那里有两个选项卡,一个带有列表,professors
另一个带有pupils
. 此窗口中提供了用于添加和删除行的相同工具。
在测试我的应用程序时,我遇到了由这一系列操作触发的奇怪行为:
- 打开两个窗口(
app delegate
管理班级和people
管理人员); - 创建一个新
class
行(此时学生和教授为零); - 删除新
class
行(假设是错误的); - 移动到
people manager
; - 随机删除
professor
一行。
应用程序崩溃并出现以下错误:
2013-10-04 09:37:40.286 Application[415:903] Serious application error. Exception was caught during Core Data change processing: CoreData could not fulfill a fault for '0x10188f020 <x-coredata://89D3EA2E-DF58-4995-8FEA-BD1D237F1B28/OfferedClasses/p45>' with userInfo {
NSAffectedObjectsErrorKey = (
"<OfferedClassesEntityModel: 0x10188a260> (entity: OfferedClasses; id: 0x10188f020 <x-coredata://89D3EA2E-DF58-4995-8FEA-BD1D237F1B28/OfferedClasses/p45> ; data: <fault>)"
);
}
现在,在互联网上搜索(我也搜索了 stackoverflow)这条消息时,我遇到了我的概念managed object context
搞砸了。但是,大多数示例似乎都在具有多个线程的应用程序中,并且两个对象上下文都没有同步。他们经常有grand central dispatch
问题,等等。我的是一个简单的窗口控制器,我不尝试在这里使用不同的对象上下文。我将尝试总结:
在我的 AppDelegate 中,我具有以下属性:
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator * persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel * managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext * managedObjectContext;
...我手动实现了以下方法:
- (NSManagedObjectModel *)managedObjectModel;
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
- (NSManagedObjectContext *)managedObjectContext;
要打开people manager
窗口,我使用以下代码:
- (IBAction)showPeopleWindow:(id)sender {
//Gets the Managed Object Context:
NSManagedObjectContext * managedObjectContext = [self managedObjectContext];
if (!peopleWindowController) {
peopleWindowController = [
[PeopleWindowController alloc]
initWithManagedObjectContext: managedObjectContext
];
}
[peopleWindowController showWindow:self];
}
该professors
列表是通过绑定处理的:在界面构建器中,我有一个array controller
用于教授的,在attributes inspector
我使用的mode
设置为Entity Name
和Entity Name
设置为Professors
withPrepares Content
并Editable
打勾。
在Bindings Inspector
我已Managed Object Context
设置为File's Owner
并Model Key Path
设置为managedObjectContext
.
该实体不会发生该错误,Pupils
并且仅在我删除一行OfferedClasses
.
OfferedClassesEntityModel
错误中提到的类是NSManagedObject
类之一。它有以下代码:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class RelationshipClassesPupilsEntityModel, ProfessorEntityModel, PupilEntityModel;
@interface OfferedClassesEntityModel : NSManagedObject
//(...)
@property (nonatomic, retain) NSSet * pupils;
@property (nonatomic, retain) ProfessorEntityModel * professor;
//(...)
@interface OfferedClassesEntityModel (CoreDataGeneratedAccessors)
- (void)addPupilsObject:(RelationshipClassesPupilsEntityModel *)value;
- (void)removePupilsObject:(RelationshipClassesPupilsEntityModel *)value;
- (void)addPupils:(NSSet *)values;
- (void)removePupils:(NSSet *)values;
//(...)
@end
所有属性都用@dynamic
.
这是PeopleWindowController
初始化方法:
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)theManagedObjectContext {
self = [super initWithWindowNibName:@"PeopleWindow"];
if (self) {
managedObjectContext = theManagedObjectContext;
}
return self;
}
请帮忙,我不知道问题出在哪里。
我试过这个:我们如何防止“CoreData 无法完成错误”?
观察者工作正常,但任何更新 MOC 的操作都不起作用。
还有一件事:它只会在新创建classes
的 . 这意味着如果我class
在之前的执行中保存了一行,它就可以正常工作。
另外:class
创建行时,它没有关系集。它professor
是空的!
谢谢!