0

我有这个使用coredata. 它有一个实体叫做OfferedClasses,一个叫做Pupils,一个叫做Professors

classes实体与其他每个实体都有关系。它可以有一个professor和几个pupils

每个实体都有一个扩展类的类表示NSManagedObject

我也有两个窗口。一个由实体拥有AppDelegate并拥有一个实体列表,其中classes包含创建选项和删除classes行的选项。

第二个窗口由我调用的一个类拥有PeopleManager。我在那里有两个选项卡,一个带有列表,professors另一个带有pupils. 此窗口中提供了用于添加和删除行的相同工具。

在测试我的应用程序时,我遇到了由这一系列操作触发的奇怪行为:

  1. 打开两个窗口(app delegate管理班级和people管理人员);
  2. 创建一个新class行(此时学生和教授为零);
  3. 删除新class行(假设是错误的);
  4. 移动到people manager;
  5. 随机删除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 NameEntity Name设置为ProfessorswithPrepares ContentEditable打勾。

Bindings Inspector我已Managed Object Context设置为File's OwnerModel 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是空的!

谢谢!

4

1 回答 1

0

如果它不是线程问题/多上下文问题,那么这是由于我要说的关系上设置了错误的删除规则。

瞳孔仍然与删除的对象有关系。

将其设置为无效

于 2013-10-04T09:36:32.043 回答