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

  • Home
  • SEARCH
  • 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 287967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:46:24+00:00 2026-05-12T05:46:24+00:00

Possible Duplicate: How to implement re-ordering of CoreData records? I’m trying to find a

  • 0

Possible Duplicate:
How to implement re-ordering of CoreData records?

I’m trying to find a code sample that shows how to handle moving/rearranging cells in a tableView when the cell uses a fetchedResultsController (i.e. in conjunction with Core Data). I’m getting the moveRowAtIndexPath: call to my data source, but I can’t find the right combination of black magic to get the table/data to recognize the change properly.

For example, when I move row 0 to row 2 and then let go, it “looks” correct. Then I click “Done”. The row (1) that had slid up to fill row 0 still has it’s editing mode appearance (minus and move icons), while the other rows below slide back to normal appearance. If I then scroll down, as row 2 (originally 0, remember?) nears the top, it completely disappears.

WTF. Do I need to somehow invalidate the fetchedResultsController? Whenever I set it to nil, I get crashes. Should I release it instead? Am I in the weeds?

Here’s what I’ve currently got in there…

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

    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];

    /*
     Update the links data in response to the move.
     Update the display order indexes within the range of the move.
     */

    if (fromIndexPath.section == toIndexPath.section) {

        NSInteger start = fromIndexPath.row;
        NSInteger end = toIndexPath.row;
        NSInteger i = 0;
        if (toIndexPath.row < start)
            start = toIndexPath.row;
        if (fromIndexPath.row > end)
            end = fromIndexPath.row;
        for (i = start; i <= end; i++) {
            NSIndexPath *tempPath = [NSIndexPath indexPathForRow:i inSection:toIndexPath.section];
            LinkObj *link = [fetchedResultsController objectAtIndexPath:tempPath];
            //[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:tempPath]];
            link.order = [NSNumber numberWithInteger:i];
            [managedObjectContext refreshObject:link mergeChanges:YES];
            //[managedObjectContext insertObject:link];
        }

    }
    // Save the context.
    NSError *error;
    if (![context save:&error]) {
        // Handle the error...
    }

}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    if (self.theTableView != nil)
        [self.theTableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    if (self.theTableView != nil) {
        [self.theTableView endUpdates];
    }
}
  • 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-05-12T05:46:24+00:00Added an answer on May 12, 2026 at 5:46 am

    Here’s what’s officially working now, with deletes, moves, and inserts. I “validate” the order any time there’s an edit action affecting the order.

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section != kHeaderSection) {
    
            if (editingStyle == UITableViewCellEditingStyleDelete) {
    
                @try {
                    LinkObj * link = [self.fetchedResultsController objectAtIndexPath:indexPath];
    
                    debug_NSLog(@"Deleting at indexPath %@", [indexPath description]);
                //debug_NSLog(@"Deleting object %@", [link description]);
    
                    if ([self numberOfBodyLinks] > 1) 
                        [self.managedObjectContext deleteObject:link];
    
                }
                @catch (NSException * e) {
                    debug_NSLog(@"Failure in commitEditingStyle, name=%@ reason=%@", e.name, e.reason);
                }
    
            }
            else if (editingStyle == UITableViewCellEditingStyleInsert) {
                // we need this for when they click the "+" icon; just select the row
                [theTableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
            }
        }
    }
    
    - (BOOL)validateLinkOrders {        
        NSUInteger index = 0;
        @try {      
            NSArray * fetchedObjects = [self.fetchedResultsController fetchedObjects];
    
            if (fetchedObjects == nil)
                return NO;
    
            LinkObj * link = nil;       
            for (link in fetchedObjects) {
                if (link.section.intValue == kBodySection) {
                    if (link.order.intValue != index) {
                        debug_NSLog(@"Info: Order out of sync, order=%@ expected=%d", link.order, index);
    
                        link.order = [NSNumber numberWithInt:index];
                    }
                    index++;
                }
            }
        }
        @catch (NSException * e) {
            debug_NSLog(@"Failure in validateLinkOrders, name=%@ reason=%@", e.name, e.reason);
        }
        return (index > 0 ? YES : NO);
    }
    
    
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
        NSArray * fetchedObjects = [self.fetchedResultsController fetchedObjects];  
        if (fetchedObjects == nil)
            return;
    
        NSUInteger fromRow = fromIndexPath.row + NUM_HEADER_SECTION_ROWS;
        NSUInteger toRow = toIndexPath.row + NUM_HEADER_SECTION_ROWS;
    
        NSInteger start = fromRow;
        NSInteger end = toRow;
        NSInteger i = 0;
        LinkObj *link = nil;
    
        if (toRow < start)
            start = toRow;
        if (fromRow > end)
            end = fromRow;
    
        @try {
    
            for (i = start; i <= end; i++) {
                link = [fetchedObjects objectAtIndex:i]; //
                //debug_NSLog(@"Before: %@", link);
    
                if (i == fromRow)   // it's our initial cell, just set it to our final destination
                    link.order = [NSNumber numberWithInt:(toRow-NUM_HEADER_SECTION_ROWS)];
                else if (fromRow < toRow)
                    link.order = [NSNumber numberWithInt:(i-1-NUM_HEADER_SECTION_ROWS)];        // it moved forward, shift back
                else // if (fromIndexPath.row > toIndexPath.row)
                    link.order = [NSNumber numberWithInt:(i+1-NUM_HEADER_SECTION_ROWS)];        // it moved backward, shift forward
                //debug_NSLog(@"After: %@", link);
            }
        }
        @catch (NSException * e) {
            debug_NSLog(@"Failure in moveRowAtIndexPath, name=%@ reason=%@", e.name, e.reason);
        }
    }
    
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {    
        @try {
            switch (type) {
                case NSFetchedResultsChangeInsert:
                    [theTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [self validateLinkOrders];
                    break;
                case NSFetchedResultsChangeUpdate:
                    break;
                case NSFetchedResultsChangeMove:
                    self.moving = YES;
                    [self validateLinkOrders];
                    break;
                case NSFetchedResultsChangeDelete:
                    [theTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [self validateLinkOrders];
                    break;
                default:
                    break;
            }
        }
        @catch (NSException * e) {
            debug_NSLog(@"Failure in didChangeObject, name=%@ reason=%@", e.name, e.reason);
        }
    }
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
        switch(type) {
            case NSFetchedResultsChangeInsert:
                [self.theTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeDelete:
                [self.theTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
    
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
        // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
        @try {
            if (self.theTableView != nil) {
                //[self.theTableView endUpdates];
                if (self.moving) {
                    self.moving = NO;
                    [self.theTableView reloadData];
                    //[self performSelector:@selector(reloadData) withObject:nil afterDelay:0.02];
                }
                [self performSelector:@selector(save) withObject:nil afterDelay:0.02];
            }   
    
        }
        @catch (NSException * e) {
            debug_NSLog(@"Failure in controllerDidChangeContent, name=%@ reason=%@", e.name, e.reason);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Reference unit tests for common data structures? I'm trying to implement the
Possible Duplicate: How to implement a change listener I know that some will use
Possible Duplicate: c++ multithread I use c++ to implement a thread class. The code
Possible Duplicate: Is it safe for structs to implement interfaces? Take this code: interface
Possible Duplicate: The fundamentals of Hash tables? I am trying to implement a Simple
Possible Duplicate: Event loop implementation for Python 3? I am trying to implement an
Possible Duplicate: Issues with ActionListener (Java) I am trying to implement action listener on
Possible Duplicate: How does Microsoft One Note 2010 implement a window that is docked
Possible Duplicate: Why use MVVM? I am trying to implement MVVM in my WPF
Possible Duplicate: Timeout on a Python function call I want to implement that when

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.