开发者

Adding single row one by one in tableview from core data of iPhone

I am working on RSS reader code where articles get downloaded and are viewable offline. The problem is only after all articles are downlaoded the tableview containing headlines gets updated. Core data is used. So e开发者_运维知识库verytime NSobjectcontext is saved , [self tableview updatebegins ] is called.The table is getting updated via fetchcontroller core data.

I tried saving NSobjectcontext everytime an article is saved but that is not updating the tableview. I want a mechanism similar to instapaper tableview where articles get saved and tableview gets updated immediately. Please help if you know the solution. Thanks in advance.


Adding code for better understanding

AppDelegate.m contains following code

- (void)feedSuccess:(ZSURLConnectionDelegate*)delegate

  NSManagedObjectContext *moc = [self managedObjectContext];

  CXMLElement *root = [document rootElement];
  CXMLElement *channel = [[root elementsForName:@"channel"] lastObject];

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:[NSEntityDescription entityForName:@"FeedItem" inManagedObjectContext:moc]];
   for (CXMLElement *item in [channel elementsForName:@"item"])    
   { 
   // push element in core data and then save context
   //Save context 
    [moc save:&error];
  ZAssert(error == nil, @"Error saving context: %@", [error localizedDescription]);
  }

this triggers the table change code in RootviewController.m

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


If you use the NSFetchedResults controller, simply use its delegate method

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

as follows:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// In the simplest, most efficient, case, reload the table view.
    [self.tableView reloadData];
}

Every time you update the underlying NSManagedObjectContext, the table will be automatically updated too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜