【发布时间】: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