使用数据库时在后台任务中更新 SQLite DB 的最佳策略

发布于 2024-10-01 08:51:00 字数 406 浏览 4 评论 0原文

我有一个允许用户查询 sqlite 数据库的应用程序。当应用程序启动时,它会下载一个 URL 以查看后台线程上是否有任何数据库更新。

不幸的是,如果用户选择在更新运行时尝试查询数据库的函数,这会导致发生锁定。

我的问题是双重的。

  1. 在这种情况下避免冲突的最佳策略是什么。

    • 将数据库复制到另一个文件,更新它并复制回来
    • 拥有某种单例操作并使用它来调度队列中的操作。
    • 循环并等待锁打开(尽管我不想阻止用户)
  2. 是否可以挂起后台线程,直到应用程序终止之前,并在应用程序终止之前运行更新。

如果有人有任何建议,我将不胜感激,因为我需要运行更新,而用户不需要停止使用该应用程序。

I have an application that allows users to query an sqlite DB. When the app starts up it downloads a URL to see if there are any database updates on a background thread.

Unfortunately this causes locks to occur if the user chooses a function that attempts to query the database whilst the update is running.

My question is twofold.

  1. What is the best strategy for avoiding conflicts in this scenario.

    • Copy the database to another file update it and copy back
    • Have some sort of singleton operating and use it to schedule operations in a Queue.
    • loop and wait for the lock to open (although I don't want to hold out the user)
  2. Is it possible to suspend the background thread until just before the app terminates and run the updates just prior to application termination.

If anyone has any advice I'd appreciate it as the I am required to run the update without the user needing to stop using the app.

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

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

发布评论

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

评论(2

行至春深 2024-10-08 08:51:00

为了回答你的问题,我相信#1第3点是针对你的iPhone特定问题的最佳解决方案。由于您无法启动另一个线程来进行后台处理,因此短暂等待(大多数程序在开始时都会这样做)是一个标准操作。

我不会尝试实现一种在应用程序终止时推送更新的方法。很多时候,当人们(包括我自己)只想继续执行其他操作时,就会停止运行应用程序。

IMO,在程序开始时等待(希望不要太长)是解决此问题的最佳方案。

To answer your question, I believe #1 point 3 is the best solution for your specific problem on the Iphone. Seeing as you're not able to start off another thread to do the background processing, having a small wait (which most programs have in the beginning) is a standard action.

I would NOT try to implement a method where the updates get pushed when the application terminates. Too many times have people stopped (myself included) running an application when they just want to move on to something else.

IMO, waiting at the program start (hopefully not too long) is the best solution to this issue.

撑一把青伞 2024-10-08 08:51:00

请澄清 - 听起来数据库查询应该是一个非常短、快速的功能,尽管 URL 下载可能是一个更长的操作。您是否在整个 URL 下载操作期间持有数据库锁?如果是这样,我认为您希望将 URL 下载作为一项操作进行 - 并且一旦收到所有数据 - 快速将其应用到您的数据库,从而在 URL 下载期间不保留数据库。

Please clarify -it sounds like the database query should be a very short, quick function, though the URL download could perhaps be a much longer operation. Are you holding database locks during the entire URL download operation? If so, I think you want to do the URL download as one operation - and once all the data is received - quickly apply it to your database, thus not holding the database during the URL download.

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