目标c中的sqlite3多线程 [英] sqlite3 multithreading in objective c

查看:263
本文介绍了目标c中的sqlite3多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用dispatch_async运行后台线程,有时我的主线程和后台线程同时访问数据库,这给了我一个数据库错误,我试图通过使用sqlite3_threadsafe()来解决它总是返回2即,我不能在两个线程中使用相同的数据库连接,我希望它返回1可以任何人在这方面帮助我

I am running a background thread in my application with dispatch_async and some times my main thread and background thread both access the database same time and this was giving me a database error and I tried to solve it by using sqlite3_threadsafe() which was always returning 2 i.e, i cannot use the same database connection in two threads and I want it to return 1 can anyone help me in this regard

推荐答案

我认为你正在采取错误的做法。无论您如何编译它,SQLite都不是可靠的线程安全的 - 请参阅常见问题解答。即使SQLite是为了线程安全而编译的,如果任何事务处于挂起状态或者任何语句仍未完成,则不能从多个线程使用相同的数据库。

I think you're pursuing the wrong approach. SQLite isn't reliably threadsafe no matter how you compile it — see the FAQ. Even if SQLite is compiled for thread safety, the same database may not be used from multiple threads if any transactions are pending or any statements remain unfinalised.

以上建议使用在我看来,Grand Central Dispatch将所有SQLite访问权限汇集到一个串行调度队列中是正确的方法,尽管我更倾向于建议您创建自己的队列而不是使用主队列,原因很简单当你想等待结果时,可以可靠地调度_sync。

The recommendations above to use Grand Central Dispatch to funnel all SQLite accesses into a serial dispatch queue are the right way to proceed in my opinion, though I'd be more likely to recommend that you create your own queue rather than use the main queue for the simple reason that you can reliably dispatch_sync when you want to wait on a result.

这篇关于目标c中的sqlite3多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆