开发者

NSFetchResultsController object refresing

I have 2 views. One display Shopping Lists from Core Data using NSFetchResultsController. The second one displays items on this lists using simple NSFetchRequests. Both views contain UITableView.

When I start app I create NSFetchResultsController (subclassed by ActiveFetchResults)

-(id)initActiveFetch{

      NSFetchRequest *request = [[NSFetchRequest alloc] init];

      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lista" inManagedObjectContext:[CoreDataHandler context]];

      [request setEntity:entity];

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"archive == 0 AND deleted == NO"];

      [request setPredicate:predicate];

      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:NO];

      NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

      [request setSortDescriptors:sortDescriptors];

      [sortDescriptors release];

      [sortDescriptor release];

      if (self=[[ActiveFetchResults alloc] 

                    initWithFetchRequest:request 

                    managedObjectContext:[CoreDataHandler context] 

                    sectionNameKeyPath:nil 

                    cacheName:nil]) 

      {

            self.delegate = self;

      }

      [request release];

      return self;

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [[self sections] count];

} 

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:section];

    return [sectionInfo numberOfObjects];

} 

- (NSInteger)numberOfRowsInSection{

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:0];

    return [sectionInfo numberOfObjects];

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

      ActiveListsCell* cell;

      cell = (ActiveListsCell*)[tableView dequeueReusableCellWithIdentifier:@"ActiveLists"]; 

      if (cell == nil) {

            cell = [[[ActiveListsCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"ActiveLists"] autorelease];

      } 

      NSManagedObject *managedObject = [self objectAtIndexPath:indexPath];

      [cell setLista: (Lista*)managedObject];

      NSLog(@"%@, %@, %d ",managedObject, [managedObject name], [[managedObject items] count]);

      cell.activeListsDelegate = self;

      [cell.roundedView setAlpha:1.0];

      cell.button.alph开发者_运维问答a = 1.0; 

      cell.counter.alpha = 1.0;

      return cell;

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self sections] objectAtIndex:section];

    return [sectionInfo name];

} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    return [self sectionIndexTitles];

} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    return [self sectionForSectionIndexTitle:title atIndex:index];

} 

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

    [self.tableView beginUpdates];

} 


- (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;

    }

} 

//-(BOOL)performFetch:(NSError **)error{

// ActiveFetchResults* ss = self;

// self = [self initActiveFetch];

// 

// self.listView = ss.listView;

// self.listView.activeFetch = self;

// [self.listView.tableView setDelegate:self];

// [self.listView.tableView setDataSource:self];

// 

// [ss release];

// return [super performFetch:error];

//} 

- (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:

                  if ([anObject isKindOfClass:[Lista class]]) {

                        Lista* lista = (Lista*)anObject;

                        if ([[lista deleted] boolValue]) {

                              [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

                                                       withRowAnimation:UITableViewRowAnimationLeft];

                        }

                  }

                  else

                        [self configureCell:[tableView cellForRowAtIndexPath:indexPath]

                                    atIndexPath:indexPath];

            break; 

        case NSFetchedResultsChangeMove:

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

                                           withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]

                                           withRowAnimation:UITableViewRowAnimationFade];

            break;

    }

} 


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

    [self.tableView endUpdates];

} 

-(void)configureCell:(UITableViewCell*)cell atIndexPath:(NSIndexPath*)indexPath{

      ActiveListsCell* activeCell = (ActiveListsCell*)cell;

      NSManagedObject *managedObject = [self objectAtIndexPath:indexPath];

      [activeCell setLista: (Lista*)managedObject];

}

When I go to second View and add new Item for selected list by adding Item object to Core data and I return to List View it still shows old number of objects. And the items property of list which should show me all Items objects for this list is an NSarray object with items count not including new added objects. But second View shows me all items with those new added too.

In few words it looks like by using NSFetchResultsController I have all object frozen from the first fetch and not responding to changes despite using "performFetch:" function. While using simple NSFetchRequest in second View everything works fine.

Can someone tell me why NSFetchResultsController objects stay frozen and do not change while core data records change?


Possible issues:

  1. Did you save NSManagedObjectContext after you updated or added objects?

  2. Try adding this case

    case NSFetchedResultsChangeUpdate: [self.tableView reloadData]; break;

in your - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type method

  1. try reloading data of tableview every time after you perform fetch:

    [self.tableView reloadData];

  2. If it still doesn't work, review the core data recipes sample code of Apple: http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜