【问题标题】:FMDB with multithread problemFMDB 多线程问题
【发布时间】:2011-05-09 22:16:20
【问题描述】:

在我的 ios 应用程序中,我使用 FMDB 连接到 sqlite3 数据库。 在app中,db会在多线程中使用,结果我是这样创建单例的:

+ (id)instance
{
static DBManager *dbManager = nil;
if (dbManager == nil) {
    dbManager = [[DBManager alloc]init];
}

[dbManager initialDBmanager];
return dbManager;
}

然而 Error: FMDataBase is current in use ,有时会发生。 然后我更新实例:

+ (id)instance
{
static DBManager *dbManager = nil;
if (dbManager == nil) {
    dbManager = [[DBManager alloc]init];
            [dbManager initialDBmanager];
}

while([dbManager.db inUse])//In my opinion this promises the db is free
    {
    }
return dbManager;
}

错误仍然存​​在。 所以我的问题是如何正确创建单例以及为什么代码无法避免错误。 谢谢!

【问题讨论】:

    标签: ios fmdb


    【解决方案1】:

    我使用@synchronized,它暂时解决了问题。

    我会继续关注的~

    【讨论】:

    • 你在哪里使用过@synthesized?
    • 现在我为 db 的每个写入操作添加一个 nslocksynthesized是个偷懒的方式~:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2016-11-09
    • 2011-10-23
    相关资源
    最近更新 更多