Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:50:52+00:00 2026-06-12T00:50:52+00:00

This code raises the CoreData: error: (19) PRIMARY KEY must be unique error. The

  • 0

This code raises the “CoreData: error: (19) PRIMARY KEY must be unique” error.
The Day entity has only a when attribute which is an NSDate, and a to-many relationship called tasks. Why this error? If a Day with a specific date is already stored, I fetch it, otherwise I insert it. So, for each day object, there should be a different when attribute. I am not sure if this is the primary key though. How to solve this ? Thank you in advance.

   NSMutableSet *occurrences = nil;

   occurrences = ... 
   NSMutableOrderedSet *newSet = [NSMutableOrderedSet orderedSetWithCapacity:[occurrences count]];

   for(NSDate *current in occurrences) {

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // try to find a corresponding Day entity whose when attribute is equal to the current occurrence
        // if none is available, create it

        Day * day = [[self getDayForDate:current inManagedObjectContext:moc] retain];
        if(!day){
            day = (Day *) [NSEntityDescription insertNewObjectForEntityForName:@"Day" inManagedObjectContext:moc];
        }

        day.when = current;
        [day addTasksObject:aTask];

        [newSet addObject:day];

        [moc insertObject:day];
        [moc processPendingChanges];

        [day release];

        [pool release];            
    } 

- (Day *)getDayForDate:(NSDate *)aDate inManagedObjectContext:(NSManagedObjectContext *)moc
{        
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:moc];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(when == %@)", aDate];
    [request setPredicate:predicate];
    NSError *error = nil;
    NSArray *array = [moc executeFetchRequest:request error:&error];
    [request release];

    Day *theDay = nil;

    if(array && [array count] == 1){
        theDay = [array objectAtIndex:0];
    }

    return theDay;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:50:53+00:00Added an answer on June 12, 2026 at 12:50 am

    I guess you don’t need to insert a new Day if you already have it (this is the case when day is not nil). In particular I’m referring to [moc insertObject:day].

    If you use insertNewObjectForEntityForName, that method inserts the object for you when you save the moc. If you need to modify it (you have retrieved a non-nil day) modify it and save. In additon, I will do processPendingChanges when the loop finishes (just for performance reasons).

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: datestring = date.min.strftime(%d-%m-%Y) date = datetime.strptime(datestring,%d-%m-%Y) This raises the following error:
This code below allows me to find the word error in all my files
This code only renders a dodecahedron and completely ignores the glBegin(GL_TRIANGLES) block: glutSolidDodecahedron(); glBegin(GL_TRIANGLES);
This code gives me this error:Cannot implicitly convert type ArrayList[] to ArrayList[][] at this
This code fails on some machines: // Parse error: syntax error, unexpected '[' ...
Looking for good explanation of why this code raises SyntaxError . def echo(x): return
I need to write a somewhat specific implementation of a cache, which has unique
This code raises segmentation fault on the last line. Header is a pointer to
The following code raises a syntax error: >>> for i in range(10): ... print
This code is the core of a much larger script that works great in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.