mercredi 1 octobre 2014

iOS - NSFetchedResultController and MagicalRecords


Vote count:

0




I've a UITableView with following code in lifecycle:



- (void)viewDidLoad {
[super viewDidLoad];

self.fetchedResultsController = [UserModel fetchAllSortedBy:@"fullName" ascending:YES withPredicate:nil groupBy:nil delegate:self];
[self updatePredicate];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.fetchedResultsController.delegate = nil;
}


and in NSFetchedResultControllerDelegate:



- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;

switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeMove:
break;

case NSFetchedResultsChangeUpdate:
break;
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}


The method [self updatePredicate]:



- (void) updatePredicate {
NSPredicate *messageFilter = [NSPredicate predicateWithFormat:@"address != nil"];
self.fetchedResultsController.fetchRequest.predicate = messageFilter;
[self.fetchedResultsController performFetch:nil];
[self.tableView reloadData];
}


To simulate the change into CoreDate I've added a simple UIBarButton event:



- (IBAction)didChangeSomething:(id)sender {
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
UserDAO *userDAO = [[UserDAO alloc] initWithContext:localContext];
UserModel *userDB = [userDAO getUserById:@"a3535ee6-645a-4f6e-9970-4df65120353e"];
userDB.address = userDB.address == nil ? @"Testing" : nil;
[localContext saveToPersistentStoreAndWait];
}];
}


I've this strange behaviour:



  1. If I open my UITableViewController for first time (when address field is nil) no rows is shown, then I click on my UIBarButton and nothing happens. (But in my Sqlite file I can see the real change of address in "Testing").

  2. If I close my UITableViewController and I re-open it, I can see the entry on TableView. Now if I click on UIBarButton the table updates correctly, adding and removing the entry more times.


Why I've this behaviour? What's happening?



asked 6 mins ago







iOS - NSFetchedResultController and MagicalRecords

Aucun commentaire:

Enregistrer un commentaire