NSFetchedResultsController with data not updating

I am making an application that retrieves a list of products from a server. Then I save them to the Core Data database and present them using GMGridView , and the data source is NSFetchedResultsController. When I change the product details on the server, I want my iOS application to synchronize and make the necessary changes, so I implement the NSFetchedResultsControllerDelegate method. How do I update gridView correctly?

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
        switch(type)
        {
            case NSFetchedResultsChangeInsert:
                [_currentData insertObject:anObject atIndex:newIndexPath.row];
                [_currentData removeObjectAtIndex:indexPath.row];
                [_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
                [_gmGridView removeObjectAtIndex:indexPath.row animated:YES];

                [_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
                [_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

                [_gmGridView reloadData];

                //[self updatePageControl];
                break;

            case NSFetchedResultsChangeDelete:
                [_currentData removeObjectAtIndex:indexPath.row];

                [_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
                //[self updatePageControl];
                [_gmGridView reloadInputViews];
                [_gmGridView reloadData];

                break;

            case NSFetchedResultsChangeUpdate:
                [_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

                ////////might be irrelevant, but just trying it out
                [_currentData insertObject:anObject atIndex:newIndexPath.row];
                [_currentData removeObjectAtIndex:indexPath.row];
                [_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
                [_gmGridView removeObjectAtIndex:indexPath.row animated:YES];

                [_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
                [_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

                ////////


                [_gmGridView reloadInputViews];
                [_gmGridView reloadData];

                break;

            case NSFetchedResultsChangeMove:
                [_currentData removeObjectAtIndex:indexPath.row];
                [_currentData insertObject:anObject atIndex:newIndexPath.row];
                [_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
                [_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];

                [_gmGridView reloadInputViews];

                [_gmGridView reloadData];

                break;
        }
}

Some information:

_currentData = [[self.fetchedResultsController fetchedObjects]mutableCopy];
//I did this because previously I wasn't using a fetchedResultsController but a NSMutableArray instead. I know that it inefficient (because I have 2 models) but this is the simplest implementation I want to do now. 

I am making changes to CoreData in another class by modifying the same UIManagedDocument distribution initiated from the same local URL.

However, I have two important issues:

  • (, ), , .. GMGridViewCell. ( , ).
  • , , , . ( , , . , ). :

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Product"];        

request.predicate = [NSPredicate predicateWithFormat:@"product_id = %@", [imonggoInfo   objectForKey:PRODUCT_ID]];    

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];    

request.sortDescriptors = [NSArray 

arrayWithObject:sortDescriptor];  NSError *error = nil;  

NSArray *matches = [context executeFetchRequest:request error:&error];

if (!matches | ([matches count] > 1)){
    //handle error
}else if ([matches count] == 0){
    //make a new product
}else{
    //return existing
    item = [matches lastObject];
}
+1
1

, NSManagedObjectContextDidSaveNotification, , , . question

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];

//

- (void)contextDidSave:(NSNotification*)notification{
    NSLog(@"contextDidSave Notification fired.");
    SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
    [self.itemDatabase.managedObjectContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:NO]; 
}
+1

Source: https://habr.com/ru/post/1777967/


All Articles