【问题标题】:Core Data Errors 1600 and 133020核心数据错误 1600 和 133020
【发布时间】:2011-09-22 13:05:47
【问题描述】:

我想我要到此为止了。我正在开发一个执行和 api 调用然后将响应存储在 Core Data 中的应用程序。我在 iphone 开发方面也很n00b,所以如果我的问题可以使用更多信息,请告诉我。

似乎每当我运行应用程序并且第一次调用 API 时,它都可以正常工作,但是当它更新核心数据中已经存在的数据时,我收到错误 130020 或 1600。我查看了它们的内容是:

1600 - NSValidationRelationshipDeniedDeleteError 133020 - NSManagedObjectMergeError

根据我的阅读,当 deleteRule 设置为拒绝时可能会出现 1600,但我的每个关系删除规则在数据模型中都设置为无效。

而对于 133020,我完全不知道该怎么办。

要注意的另一件有趣的事情是,对于这些错误何时弹出似乎没有任何逻辑(我可以确定)。一次可能是 1600,一次是 133020,另一次可能是完全没有,对于同一序列中的同一操作。

如果有人能给我一些关于我可以查看的内容,甚至我可以如何尝试调试它的指示,我将不胜感激。

我粘贴了以下 2 段代码,它们似乎始终会导致至少一个错误:

- (void)updatePhotoAlbumsForGroup:(NSNumber *)gid notifyRegistrant:(id <APRegistrant>)registrant {
  NSManagedObjectContext *context = [[self newContextWithSharedCoordinator] autorelease];
  [self registerObserver:registrant selector:@selector(managedObjectContextDidSave:) forDidSaveNotificationFromContext:context];

  APGroup *group = [APCoreDataRequests fetchGroupWithGID:gid inContext:context];

  // Download and create new albums
  [self queueBlock:^{
    NSArray *drupalAlbums = [_drupalRequests getPhotoAlbumsForGroup:gid];
    if ([drupalAlbums count]) {
      [self refreshPhotoAlbumsForGroup:group withDrupalAlbums:drupalAlbums inContext:context];

      // Save any changes
      dispatch_async(dispatch_get_main_queue(), ^{
        [self saveContext:context];
      });
    }
  }];
}

这是上面调用的 refreshphotoalbumsforgroup 函数:

/**
 Uses the drupal albums to create new albums, update existing ones, and delete dead ones.
 */
- (void)refreshPhotoAlbumsForGroup:(APGroup *)group withDrupalAlbums:(NSArray *)drupalAlbums inContext:(NSManagedObjectContext *)context {
  NSMutableArray *newDrupalAlbums = [NSMutableArray arrayWithArray:drupalAlbums];
  NSMutableArray *existingAlbumsToDelete = [NSMutableArray array];

  // Update existing albums and collect albums to be deleted and created
  if (group.photoAlbums) {
    for (APPhotoAlbum *album in group.photoAlbums) {
      BOOL deleteAlbum = YES;

      for (NSDictionary *drupalAlbum in drupalAlbums) {
        NSNumber *aid = [drupalAlbum objectForKey:kGetPhotoAlbumsAIDKey];
        if (aid) {
          if ([aid isEqualToNumber:album.aid]) {
            deleteAlbum = NO;

            // Update existing album
            NSString *title = [drupalAlbum objectForKey:kGetPhotoAlbumsTitleKey];
            if (title) album.title = title;
            NSString *repPhotoURL = [drupalAlbum objectForKey:kGetPhotoAlbumsRepresentativePhotoURLKey];
            if (repPhotoURL) album.representativePhotoURL = repPhotoURL;
            NSNumber *numPhotos = [drupalAlbum objectForKey:kGetPhotoAlbumsNumberOfPhotosKey];
            if (numPhotos) album.numberOfPhotos = numPhotos;
            NSNumber *dateNum = [drupalAlbum objectForKey:kGetPhotoAlbumsModificationDateKey];
            if (dateNum) album.modificationDate = [NSDate dateWithTimeIntervalSince1970:[dateNum doubleValue]];
            NSNumber *radioactivity = [drupalAlbum objectForKey:kGetPhotoAlbumsRadioactivityKey];
            if (radioactivity) album.radioactivity = radioactivity;

            [newDrupalAlbums removeObject:drupalAlbum];
            break;
          }
        }
        else {
          [newDrupalAlbums removeObject:drupalAlbum];
        }
      }

      if (deleteAlbum) {
        [existingAlbumsToDelete addObject:album];
      }
    }
  }

  // Delete albums
  for (APPhotoAlbum *album in existingAlbumsToDelete) {
    [context deleteObject:album];
  }

  // Create and add new albums
  NSMutableSet *currentAlbums = [NSMutableSet setWithSet:group.photoAlbums];
  for (NSDictionary *drupalAlbum in newDrupalAlbums) {
    NSNumber *aid = [drupalAlbum objectForKey:kGetPhotoAlbumsAIDKey];
    APPhotoAlbum *album = [APCoreDataRequests fetchPhotoAlbumWithAID:aid inContext:context];

    // Set album properties
    NSString *title = [drupalAlbum objectForKey:kGetPhotoAlbumsTitleKey];
    if (title) album.title = title;
    NSString *repPhotoURL = [drupalAlbum objectForKey:kGetPhotoAlbumsRepresentativePhotoURLKey];
    if (repPhotoURL) album.representativePhotoURL = repPhotoURL;
    NSNumber *numPhotos = [drupalAlbum objectForKey:kGetPhotoAlbumsNumberOfPhotosKey];
    if (numPhotos) album.numberOfPhotos = numPhotos;
    NSNumber *dateNum = [drupalAlbum objectForKey:kGetPhotoAlbumsModificationDateKey];
    if (dateNum) album.modificationDate = [NSDate dateWithTimeIntervalSince1970:[dateNum doubleValue]];
    NSNumber *radioactivity = [drupalAlbum objectForKey:kGetPhotoAlbumsRadioactivityKey];
    if (radioactivity) album.radioactivity = radioactivity;

    [currentAlbums addObject:album];
  }
  group.photoAlbums = [NSSet setWithSet:currentAlbums];
}

【问题讨论】:

  • 能否请您发布一些代码,以便我们看看您在做什么。
  • 当然——刚刚更新了我的问题

标签: iphone objective-c core-data xcode4


【解决方案1】:

尝试设置托管对象上下文的mergePolicy,例如到NSMergeByPropertyObjectTrumpMergePolicy。它默认为NSErrorMergePolicy,如果多个上下文编辑同一个对象,它总是会导致验证错误。

【讨论】:

  • 对不起,我是超级 n00b。我实际上是从其他人那里接管了该应用程序。您介意更具体地说明我在哪里/如何做到这一点吗?
  • 所以我将此权利添加到 refreshPhotoAlbumsForGroup 函数的顶部,但仍然遇到相同的错误。 [上下文 setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
  • 并且还在 updatePhotoAlbumsForGroup 函数的顶部尝试过 - 仍然没有骰子:(
  • 实际上现在我看,有趣的是它不再抛出 13302 - 只有 1600。所以也许它确实有效。我知道它试图删除一个组中的一个相册,这是一种关系。但是 del 规则设置为无效
  • 您必须为所有您的托管对象上下文设置合并策略。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2011-10-30
  • 2014-07-05
  • 2012-03-28
  • 2012-02-16
相关资源
最近更新 更多