iPhone & SQLite - 丢失数据库连接?

发布于 2024-11-28 22:51:13 字数 866 浏览 0 评论 0原文

我的应用程序中出现这个奇怪的错误已经有一段时间了,也许你们可以帮助我。

这是给定的:

  1. 应用程序下载(大量)图像并将它们存储在 Library/Caches 目录中。

  2. 对于下载的每个图像,URL 和文件名都存储在 SQLite 数据库中。正如您所看到的,我重新发明了轮子并创建了我自己的“缓存”系统。我这样做是为了帮助减少应用程序在必须下载大量图像时的内存占用。

  3. 我有一个 UITableViewController,它在其单元格中显示一些文本数据以及(一些)下载的图像。每次调用以下命令时都会从数据库中查询文件名:

    - (UITableViewCell*) tableView: (UITableView*) tableView
             cellForRowAtIndexPath: (NSIndexPath*) 索引路径
    

查询的形式为:

SELECT filename, imageTitle, etcetera
FROM tbl_images
WHERE url = 'http://www.foo.com/bar'

问题来了:

在 tableView 上快速向上或向下滚动时,从而导致在短时间内执行大量查询有时,应用程序“中断”并且查询返回空行,并且除非您重新启动它,否则永远不会再次正常工作。我猜数据库连接以某种方式丢失了,但我不能确定。

任何人都知道发生了什么和/或如何解决它?

编辑

好的,我刚刚找到了错误的原因。当它被重现时,我发现数据库已被某种方式损坏。

不过,感谢您的评论,并抱歉浪费了您的时间。 v^_^'

I've had this weird bug in my application for a while, maybe you guys can help me.

Here's the given:

  1. The application downloads (a lot of) images and stores them in the Library/Caches directory.

  2. 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.

  3. 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 really 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^_^'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-12-05 22:51:13

那么您可以将数据库中的项目加载到数据源中,数据源可能是数组的数组。每个子数组都包含特定行的信息。每当下载新文件时,只需更新文件路径。然后,您可以将图像存储到磁盘并从文件路径读取。如果数据为 NULL,您可以显示活动指示器,直到它被下载。

在下载方法中,您可以传递一个值来指示图像应适合哪一行。下载完成后,您可以更新文件路径。这样做您将减少/消除在数据库中缓存图像的需要。

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.

吃兔兔 2024-12-05 22:51:13

您是否检查了 sqlite3_prepare_v2 的返回代码?您还可以缓存准备好的语句,以使其快速填充到数据库。您每次请求此信息时是否打开数据库连接?

如果没有使用数据库的代码,很难说出为什么会得到这样的图片。

您可以使用“惰性”方法来表示数据。例如,在 UIScrollViewDelegate *scrollViewDidEndDragging:willDecelerate:* 和 scrollViewDidEndScrollingAnimation: 方法中调用数据库中的数据并填充没有数据的行(或使用一些最小的数据)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文