IOS crash CoreData: error: (19) PRIMARY KEY must be unique

I just integrated the core data into the application, and when the user launches it for the first time, I create a core and start adding information from XML, here is the code I am using:

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);
    }
}

      

ideasResponseDict is an XML format in a dictionary.

My app crashes on line:

if (![context save:&managedObjError])

      

If I don't understand what I am doing wrong, because the same code works if the user logs in or logs in, and the master data is generated when they log into the application, but the same code fails if the user logs in, logs in application and then it queries the data manually and the master data is generated.

Before integrating crashlytics to improve logs, I was getting this report when the app crashed:

global ID may not be temporary when recording

      

Update. I changed the code to:

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);
    }

      

And now it works, I don't know if the problem was that I saved this context a lot, but it currently works.

+3


source to share





All Articles