Search code examples
iphoneobjective-ciosicloudnsset

iCloud doesn't support ordered sets.. alternative solution?


iCloud doesn't support ordered sets for relationships, only sets. Right now, i've made an attribute called 'entryIndex', where it stores it's own index value that it's given. When the user deletes the object at a specific index, I want to identify the object and change the value of the objects with higher indexes. How do I do this?


Solution

  • Assuming you have consecutive indexes and want to keep them "tight" without gaps:

    NSArray *filtered = [fetchedObjects filteredArrayUsingPredicate:
       [NSPredicate predicateWithFormat:@"entryIndex > %@", 
                                        deletedObject.entryIndex]];
    for (NSManagedObject *obj in filtered) {
       obj.entryIndex = [NSNumber numberWithInt:[obj.entryIndex intValue]-1];
    }
    [self.managedObjectContext save:nil];