Chris
Chris

Reputation: 21

How to keep an SQLite database and UITableView synchronized

I'm new to iPhone development and try to create a simple address book. The contact data is stored in an SQLite database and presented in a UITableView (just like in the Contacts app). When the user taps on a name in the table, a detail view becomes visible. There's a delete button that deletes the contact.

The problem is, that I'm not sure how to synchronize the SQLite database with the data source of the table. All contact data is stored in an NSMutableArray that becomes populated when the app starts (using a SELECT query). Now, when I delete a contact, I have to delete both the entry in the array and the row in the database.

Is there any other, more 'elegant' way to do this?

Upvotes: 2

Views: 558

Answers (2)

Dan Ray
Dan Ray

Reputation: 21893

I've resolved this by putting [myTableView reloadData] inside -viewWillAppear. That way every single time this view comes into view--whether pushed on or popped-to--it'll re-up its data source.

Upvotes: 2

Greg
Greg

Reputation: 33650

You may want to try using Core Data with an NSFetchedResultsController. If you create a navigation based application in Xcode and click on the Core Data checkbox, it will create an example application that is backed by Core Data. I used this when I set up my first application that used a navigation controller and table view with core data, and it gives a great starting point with the ability to add and delete rows in the table view.

Upvotes: 3

Related Questions