Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way I resolved this is with Core Data aggregates. I actually end up assigning the ID myself. </p> <p>Essentially, I query Core Data for all of the entity IDs of my entity and then iterate through them. If I find an ID which is higher than the current temporary one, I make the temporary ID higher one higher than the aggregated one. When I'm done, I automatically have an ID which is higher than the highest one in the list. The only flaw I see with this is if there is a missing ID. (I believe that there is a simple fix for this as well.)</p> <pre><code>// // Create a new entity description // NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext]; // // Set the fetch request // NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; [fetchRequest setEntity:entity]; // // We need to figure out how many // existing groups there are so that // we can set the proper ID. // // To do so, we use an aggregated request. // [fetchRequest setResultType:NSDictionaryResultType]; [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"entityID"]]; NSError *error = nil; NSArray *existingIDs = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error]; if (error != nil) { // // TODO: Handle error. // NSLog(@"Error: %@", [error localizedDescription]); } NSInteger newID = 0; for (NSDictionary *dict in existingIDs) { NSInteger IDToCompare = [[dict valueForKey:@"entityID"] integerValue]; if (IDToCompare &gt;= newID) { newID = IDToCompare + 1; } } // // Create the actual entity // MyEntity *newEntity = [[MyEntity alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext]; // // Set the ID of the new entity // [newEntity setEntityID:[NSNumber numberWithInteger:newID]]; // // ... More Code ... // </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload