2011-06-08 54 views
0

我有2個視圖。使用NSFetchResultsController從核心數據顯示購物清單。第二個使用簡單的NSFetchRequests顯示這個列表上的項目。這兩個視圖都包含UITableView。NSFetchResultsController對象刷新

當我開始應用程序創建NSFetchResultsController(由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.alpha = 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]; 

} 

當我去到第二查看和加入項目對象的核心數據增加新的項目選定的名單,我返回列表查看它仍然顯示舊數目的對象。列表的items屬性應顯示此列表的所有Items對象,其中items數不包括新添加的對象的NSarray對象。但第二個視圖向我展示了所有添加了這些新項目的項目。

簡而言之,它看起來像使用NSFetchResultsController儘管使用了「performFetch:」函數,但我已經從第一次獲取中凍結了所有對象,並且未響應更改。在第二個視圖中使用簡單的NSFetchRequest時,一切正常。

有人能告訴我爲什麼NSFetchResultsController對象保持凍結狀態,並且在覈心數據記錄更改時不會更改?

回答

1

可能的問題:

  1. 你節省NSManagedObjectContext你更新或添加對象後?

  2. 嘗試增加這種情況下

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

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

  1. 嘗試重新加載tableview中的數據,每次執行後獲取:

    [self.tableView reloadData];

  2. 如果還是不行,請查閱蘋果公司的核心數據食譜示例代碼:http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html