views:

105

answers:

1

I'm having trouble understanding coredata with my uitableview. I have rows being deleted and added all ok.. and the tableview displays them. However, when I then run a query on the number of elements in the database, it returns the number BEFORE I made the delete call.

So an example, I have three rows. I delete two of them. I'd expect my code to return 1 (1 row left) but it returns 3. Do I need to update the record set or something? ie: the delete is made, but not committed or something?

Here's the code I use to query the number of rows... (and the reason I need this is to run operations on the uitableview... so the number of rows in the table should match the number returned from my function)

NSInteger numberofRows = 0;
NSInteger section = 0;

id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
numberofRows = [sectionInfo numberOfObjects];

NSLog(@"total rows=%i",numberofRows);

So my method is to

  1. there are 3 rows in uitableview and the db
  2. delete selected rows (eg 2)
  3. reload table view (shows the one remaining row)
  4. count number of rows in DB (returns 3)

Do I need to refresh the connection or something?? thanks for any help!

+1  A: 

Depending on how the rows were deleted you may need to call NSManagedObjectContext's save: method. That will commit any unsaved changes to the store.

theMikeSwan
used a version of this idea which worked - thanks!
Matt Facer