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
ios objective-c core-data


source to share


No one has answered this question yet

Check out similar questions:

3
Core Data error: _Unwind_Resume called from _PFFaultHandlerLookupRow function in CoreData image
2
FaceBook Connect. Login to the system
2
_PFFaultHandlerLookupRow error while saving child context
2
Error while saving NSManagedObjectContext in swift
1
Crash when reusing NSFetchRequest in multiple contexts
1
managedObjectModel in nil (WatchApp only)
1
Getting error when fetching followers using FHSTwitterEngine
0
executefetchrequest crashes
0
Displaying Data Core Data records with a 1: M ratio
0
Retrieving data from master data with multiple data



All Articles
Loading...
X
Show
Funny
Dev
Pics