iPhone & SQLite - Lost Database Connection?
I've had this weird bug in my application for a while, maybe you guys can help me.
Here's the given:
The application downloads (a lot of) images and stores them in the Library/Caches directory.
For every image downloaded, the url and filename is stored in an SQLite database. As you can see, I've re-invented the wheel and created my own "caching" system. I've done this to help reduce the memory footprint of the application when it has to download a lot of images.
I have a UITableViewController that displays some text data, as well as (some of) the downloaded images, in its cells. The filename is queried from the database everytime the following is called:
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
The queries are of the form:
SELECT filename, imageTitle, etcetera
FROM tbl_images
WHERE url = 'http://www.foo.com/bar'
Here comes the problem:
When scrolling up or down really fast on a tableView, thereby causing a lot of queries to be executed in a short amount of time, the app "breaks" and the queries return empty rows, and never works correctly again unless you restart it. I'm guessing the database connection is lost somehow or something, but I can't 开发者_如何学Pythonreally be sure.
Anyone have an idea on what is happening and/or how to fix it?
EDIT
OK guys, I just found the cause of the bug. When it is reproduced, I found that the database has been corrupted somehow.
Thanks for the comments, though, and sorry for wasting your time. v^_^'
Well you can load the items from the data base into a datasource, may be a array of arrays. Each subarray will have the information for a particular row. Whenever new file is downloaded just update the file path. You can then store the images to disk and read from the file path. If the data is NULL you can show a activity indicator untill it gets downloaded.
In your downloading method, you can pass a value which indicates which row the image should fit into. When download completes you can update the filepath. Doing so you will reduce/remove the need to cache the images in a database.
Did you check return codes from sqlite3_prepare_v2? Also you may cache prepared statement to make it very fast on population to database. Did you open database connection each time when requesting this information?
Without code for working with database it's hard to tell why you get such picture.
You can use "lazy" method to represent data. For example in UIScrollViewDelegate *scrollViewDidEndDragging:willDecelerate:* and scrollViewDidEndScrollingAnimation: methods call for data from database and fill rows w/o data (or with some minimal data)
精彩评论