I want to display several hundred records from a database in a UITableView
. Though I could use CoreData, I decided to try using a SQLite database since I'm currently attending a lecture on databases. And although I possibly could load all of the data into an array, I want to minimize memory usage so that I can use sort of the same code for another application with more data as well. So my question is, how do I load the rows from the database which are currently displayed in the table view? The table I want to display has several attributes, the primary key is a string - the table should be sorted by this string. So how do I find out, what cells are displayed (indexes) and should I then use select * from mytable limit x,y
to obtain the rows I want to display or can I improve performance in any way (either with SQLite or with some great code).
Thanks for your help in advance!
BTW: I'm using FMDB if this doesn't make any difference.
What you are trying to do is, in a way, absurd. NSFetchedResultsController
which is part of Core Data, does all the optimizations for you exactly for displaying 100s of 1000s of rows in a table view.
But as it seems you are doing this as an academic exercise, here are some hints:
numberOfRowsInSection
the total number of records. It will only request the records it needs to display. indexPath.row
.indexPathsForVisibleRows
in case you need to check what is on the screen. scrollViewDidScroll
and do the proper calculations of contentOffset
and set the table's contentOffset
on the fly to reflect the correct part of the table.