title: Cocoa Error 19 related to Core Data
body: <p>I have been doing extensive research over the past day to solve the error I have come across, but have been unable to find solutions to my problem. My app is designed to recording data as "datapoints" in a "recording" and store these objects in core data. After a recording finishes (all of the datapoints and the recording itself have already been created and added to the MOC) I save the context:</p>
  
  <pre><code> - (void)saveContext
  {
      NSError *error;
      if (self.managedObjectContext != nil) {
          if ([self.managedObjectContext hasChanges] &amp;&amp; ![self.managedObjectContext save:&amp;error]) {
              NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
              abort();
          }
      }
  }
  </code></pre>
  
  <p>Again, I have used this exact code in previous projects and it has worked without a hitch. However, with this project the app calls the <code>abort()</code> function in <code>- (void)savecontext</code>. When <code>abort()</code> is called, the following error is displayed:</p>
  
  <pre><code>CoreData: error: (19) PRIMARY KEY must be unique
  Unresolved error Error Domain=NSCocoaErrorDomain Code=19 "The operation couldn’t be completed. (Cocoa error 19.)" UserInfo=0x155c6850 {NSSQLiteErrorDomain=19, NSUnderlyingException=error during SQL execution : PRIMARY KEY must be unique, NSFilePath=/var/mobile/Containers/Data/Application/9C5CB2CF-70C3-462B-BD26-E75AC4AF22E5/Documents/APPNAME.sqlite}, {
      NSFilePath = "/var/mobile/Containers/Data/Application/9C5CB2CF-70C3-462B-BD26-E75AC4AF22E5/Documents/APPNAME.sqlite";
      NSSQLiteErrorDomain = 19;
      NSUnderlyingException = "error during SQL execution : PRIMARY KEY must be unique";
  }
  </code></pre>
  
  <p>Midway through the project, I changed the name of one of the entities. However, I doubt this is the source of the problem, because when this problem arose I remade the entire data model and all of the categories from scratch, and it changed nothing.</p>
  
  <p>In my research, I found that this error is very common in apps which pre-populate their SQLite files with data to load into the app. This is not the case with my app, however, and so I found all of the solutions to this problem on Stack Exchange to be unhelpful, as they all focused on editing this pre-existing file.</p>
  
  <p>I'm also relatively new to Stack Exchange, so if there is any piece of information which I excluded, please let me know and I'll add it. Thanks for any help you can give.</p>