核心数据NSValidationErrorObject导致崩溃

时间:2013-03-07 21:36:32

标签: ios objective-c cocoa-touch core-data

我正在开发一个我拥有用户的应用程序,每个用户与关注者之间存在多对多的关系。我目前在保存新用户对象时遇到一些问题。该应用程序在managedObjectContext save上崩溃。我收到以下错误:

Unresolved error Error Domain=NSCocoaErrorDomain Code=1560 "The operation couldn’t be   completed. (Cocoa error 1560.)" UserInfo=0x8580640 {NSDetailedErrors=(
"Error Domain=NSCocoaErrorDomain Code=1550 \"The operation couldn\U2019t be completed.  (Cocoa error 1550.)\" UserInfo=0x85820c0 {NSValidationErrorObject=<User: 0x7575870> (entity:  User; id: 0x75757c0 

NSValidationErrorKey=followers, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1550.), NSValidationErrorValue=Relationship 'followers' on managed object (0x7575870) <User: 0x7575870> (entity: User; id: 0x75757c0 
"Error Domain=NSCocoaErrorDomain Code=1550 \"The operation couldn\U2019t be completed. (Cocoa error 1550.)\" UserInfo=0x8586830 {NSValidationErrorObject=<User: 0x7571ec0> (entity: User; id: 0x7571f00 

"Error Domain=NSCocoaErrorDomain Code=1550 \"The operation couldn\U2019t be completed. (Cocoa error 1550.)\" UserInfo=0x858e820 {NSValidationErrorObject=<User: 0x7573120> (entity: User; id: 0x7573160  

[...]

我无法弄清楚导致此次崩溃的原因。我的关系看起来像这样:

enter image description here

enter image description here

该模型具有@property (nonatomic, retain) NSSet *followers;的自定义NSManagedObject子类。正如我所说,我不确定是什么导致了这一点,所以任何指导或想法都会很棒!

更新

应用程序在此方法中崩溃:

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    [managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You   should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            // Uncomment "abort" makes it work, but I still get the error. 
            abort();
        }
    }
} 

更新2

我的模型中的更多代码以及它们如何使用它们:

我如何设置我的获取请求控制器:

- (NSSortDescriptor *)sortDescriptorForFetchRequest
{
    NSSortDescriptor *sortDescriptor;
    if ([self.postType isEqualToString:@"following"] || [self.postType isEqualToString:@"followers"]) {
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userId" ascending:NO];
    } else {
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"postId" ascending:NO];
    }

    return sortDescriptor;
}

- (NSEntityDescription *)entityForFetchRequest
{
    NSEntityDescription *entity;
    if ([self.postType isEqualToString:@"followers"] || [self.postType isEqualToString:@"following"]) {
        entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:[self.appController managedObjectContext]];
    } else {
        entity = [NSEntityDescription entityForName:@"Post" inManagedObjectContext:[self.appController managedObjectContext]];
        }

    return entity;
}

- (void)setUpFetchResultController
{
    if (self.fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:[self entityForFetchRequest]];
        [fetchRequest setPredicate:[self predicateBasedOnPostType:self.postType]];
        NSArray *sortDescriptors = @[[self sortDescriptorForFetchRequest]];
        [fetchRequest setSortDescriptors:sortDescriptors];

        self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self.appController managedObjectContext] sectionNameKeyPath:nil cacheName:[self cacheName]];
        self.fetchedResultsController.delegate = self;
    }
}

我的模特,User

+ (void)addUserFromDictionary:(NSDictionary *)dictionary forUser:(User *)user  inManagedObjectContext:(NSManagedObjectContext*)moc follower:(BOOL)follower following: (BOOL)following
{
    User *localUser;
    if (follower) {
        if ([dictionary isKindOfClass:[NSArray class]]) {
            NSEnumerator *enumerator = [dictionary objectEnumerator];
            id value;
            while (value = [enumerator nextObject]) {
                localUser = [self parseUserFromDictionary:value inManagedObjectContext:moc];

               if ([self user:localUser alreadyFollowingUser:user inManagedObjectContext:moc] == NO) {
                   [user addFollowersObject:localUser];
                   [localUser addFollowingObject:user];
              }   
            }
        }
    }
}

+ (User *)parseUserFromDictionary:(NSDictionary *)dictionary inManagedObjectContext:(NSManagedObjectContext*)moc
{
    NSNumberFormatter *numberFormatter= [[NSNumberFormatter alloc] init];
    NSNumber * userId = [numberFormatter numberFromString:(NSString *)[dictionary valueForKey:@"id"]];

    User *user;

    if ([self userAlreadyExist:userId inManagedObjectContext:moc] == NO) {
        NSEntityDescription *userDescription = [NSEntityDescription entityForName:@"User" inManagedObjectContext:moc];
        user = [[User alloc] initWithEntity:userDescription insertIntoManagedObjectContext:moc];
    } else {
        user = [User findUser:userId inManagedObjectContext:moc];
    }

    user.name = [dictionary valueForKey:@"name"];
    [...]    
    return user;
}

3 个答案:

答案 0 :(得分:4)

预感:不同MOC中对象之间的关系是什么?如果你这样做,就会发生奇怪的事情!

答案 1 :(得分:3)

我在一个单独的项目中复制了您的代码和数据模型。我认为这可能是您数据中的一个循环,所以我尝试了以下所有代码:

[userA addFollowersObject:userB];
[userB addFollowingObject:userA];

[userB addFollowersObject:userA];
[userA addFollowingObject:userB];

[userA addFollowersObject:userA];
[userA addFollowingObject:userA];

[userB addFollowingObject:userB];
[userB addFollowersObject:userB];

...但保存一直有效。我能想到的最后一件事是多线程。您是否正在使用多个线程来访问ManagedObjectContext?如果是这样,可能会导致数据不一致,从而导致错误。

答案 2 :(得分:1)

如果您提供有关模型定义的更多详细信息,包括UserFollowers,将会很有帮助。这是一个猜测,但是followers不正确的反转?

我希望看到用户定义的关系如下:

M followers    User    following

和Follower中的关系定义如下:

M following    User    followers