iOS崩溃CoreData:错误:(19)PRIMARY KEY必须是唯一的

时间:2015-05-06 08:44:01

标签: ios objective-c core-data

我只是将核心数据集成到应用程序中,当用户第一次启动它时,我创建核心并开始从XML添加信息,这是我正在使用的代码:

for (int count = 0; count < suggestionsResponseDict.count; count++) {

    NSLog(@"add core data element at position: %d",count);

    SuggestedChannelsEntity *suggestedCellData = (SuggestedChannelsEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"SuggestedChannelsEntity" inManagedObjectContext:context];
    [suggestedCellData setGroupID:[NSString stringWithFormat:@"%d",count]];
    [suggestedCellData setGroupName:[NSString stringWithFormat:@"%@",[[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Name"]]];
    [suggestedCellData setGroupTags:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Tags"]]];
    [suggestedCellData setGroupNumberOfMembers:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Members"]]];
    [suggestedCellData setGroupAvatarThumbnail:@"null"];
    [suggestedCellData setGroupAvatarThumbnailLink:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"AvatarThumnail"]]];
    [suggestedCellData setGroupAvatar:@"null"];
    [suggestedCellData setGroupAvatarLink:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Avatar"]]];
    [suggestedCellData setGroupAvatar:@"null"];
    [suggestedCellData setGroupIsMember:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"permission"]]];

    NSError *managedObjError = nil;

    if (![context save:&managedObjError]) {
        NSLog(@"Next error ocured:%@ while uploading element:%d", managedObjError, count);
    }
}

suggestionsResponseDict是字典格式的XML。

我的应用程序崩溃了:

if (![context save:&managedObjError])

如果我不明白我做错了什么,因为如果用户注册或登录并且在他进入应用程序时创建了核心数据,相同的代码会起作用,但是如果用户登录,相同的代码会崩溃,进入应用程序然后他手动请求数据并创建核心数据。

在为更好的日志集成crashlytics之前,我在应用程序崩溃时收到了这些报告:

global ID may not be temporary when recording

更新我更改了代码:

for (int count = 0; count < suggestionsResponseDict.count; count++) {

    NSLog(@"add core data element at position: %d",count);

    SuggestedChannelsEntity *suggestedCellData = (SuggestedChannelsEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"SuggestedChannelsEntity" inManagedObjectContext:context];
    [suggestedCellData setGroupID:[NSString stringWithFormat:@"%d",count]];
    [suggestedCellData setGroupName:[NSString stringWithFormat:@"%@",[[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Name"]]];
    [suggestedCellData setGroupTags:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Tags"]]];
    [suggestedCellData setGroupNumberOfMembers:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Members"]]];
    [suggestedCellData setGroupAvatarThumbnail:@"null"];
    [suggestedCellData setGroupAvatarThumbnailLink:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"AvatarThumnail"]]];
    [suggestedCellData setGroupAvatar:@"null"];
    [suggestedCellData setGroupAvatarLink:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"Avatar"]]];
    [suggestedCellData setGroupAvatar:@"null"];
    [suggestedCellData setGroupIsMember:[NSString stringWithFormat:@"%@", [[suggestionsResponseDict objectAtIndex:count] objectForKey:@"permission"]]];

}

    NSError *managedObjError = nil;

    if (![context save:&managedObjError]) {
        NSLog(@"Next error ocured:%@ while uploading element:%d", managedObjError, count);
    }

现在它有效,我不知道问题是我是否经常保存上下文,但现在它可以正常工作。

0 个答案:

没有答案