views:

999

answers:

2

Hi All, I'm having some trouble getting my Core Data entities to play nice and order when using an UITableView.

I've been through a number of tutorials and other questions here on StackOverflow, but there doesn't seem to be a clear or elegant way to do this - I'm really hoping I'm missing something.

I have a single Core Data entity that has an int16 attribute on it called "displayOrder". I use an NSFetchRequest that has been sorted on "displayOrder" to return the data for my UITableView. Everything but reordering is being respected. Here is my (inefficient) moveRowAtIndePath method:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {         

    NSUInteger fromIndex = fromIndexPath.row;  
    NSUInteger toIndex = toIndexPath.row;

    FFObject *affectedObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];  
    affectedObject.displayOrderValue = toIndex;

    [self FF_fetchResults];


    for (NSUInteger i = 0; i < [self.fetchedResultsController.fetchedObjects count]; i++) {  
        FFObject *otherObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:i];  
        NSLog(@"Updated %@ / %@ from %i to %i", otherObject.name, otherObject.state, otherObject.displayOrderValue, i);  
        otherObject.displayOrderValue = i;  
    }

    [self FF_fetchResults];  
}

Can anyone point me in the direction of a good bit of sample code, or see what I'm doing wrong? The tableview display updates OK, and I can see through my log messages that the displayOrder property is being updated. It's just not consistently saving and reloading, and something feels very "off" about this implementation (aside from the wasteful iteration of all of my FFObjects).

Thanks in advance for any advice you can lend.

+6  A: 

I took a look at your code and this might work better:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {         

    NSUInteger fromIndex = fromIndexPath.row;  
    NSUInteger toIndex = toIndexPath.row;

    FFObject *affectedObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];  
    affectedObject.displayOrderValue = toIndex;

    NSUInteger start, end;
    int delta;

    if (fromIndex < toIndex) {
     // move was down, need to shift up
     delta = -1;
     start = fromIndex + 1;
     end = toIndex;
    } else { // fromIndex > toIndex
     // move was up, need to shift down
     delta = 1;
     start = toIndex;
     end = fromIndex - 1;
    }

    for (NSUInteger i = start; i <= end; i++) {
     FFObject *otherObject = [self.fetchedResultsController.fetchedObjects objectAtIndex:i];  
     NSLog(@"Updated %@ / %@ from %i to %i", otherObject.name, otherObject.state, otherObject.displayOrderValue, otherObject.displayOrderValue + delta);  
     otherObject.displayOrderValue += delta;
    }

    [self FF_fetchResults];  
}
gerry3
Brilliant! This worked perfectly. I'd very much been staring at the original code for too long - thankyou for your help.
Tony Arnold
This should be implemented in parallel with http://stackoverflow.com/questions/1077568/how-to-implement-re-ordering-of-coredata-records/2013070#2013070
QAD
If items can be deleted, this doesn't work. You have to make sure to update the order field also when deleting a row.
Kamchatka
+1  A: 

(This is intended as as comment on gerry3's answer above, but I am not yet able to comment on other users' questions and answers.)

A small improvement for gerry3's - very elegant - solution. If I'm not mistaken, the line

otherObject.displayOrderValue += delta;

will actually perform pointer arithmetic if displayOrderValue is not of primitive type. Which may not be what you want. Instead, to set the value of the entity, I propose:

otherObject.displayOrderValue = [NSNumber numberWithInt:[otherObject.displayOrderValue intValue] + delta];

This should update your entity property correctly and avoid any EXC_BAD_ACCESS crashes.

octy
displayOrder is a Core Data entity attribute (thus, NSNumber) - I'm using mogenerator, which automatically creates primitive value accessors suffixed by "Value", so in this instance the pointer arithmetic is perfectly fine.Thanks for pointing out the trap though ;)
Tony Arnold
np and... Thanks for pointing me toward mogenerator!!!
octy