c# – sqlite.net monotouch = SIGSEGV崩溃

我们使用以下内容

> Xamarin 3(Xamarin表格)
> MonoTouch
> sqlite.net
> iOS模拟器/硬件

该应用程序与后台线程上的服务器同步数据.整个应用程序只共享一个sqlite连接对象.前台查询后台同步运行的同时执行.所有这些在应用程序的Windows 8.1版本上都可以正常工作(即在MSFT Surface和类似版本上).然而,一旦我们切换到Xamarin / mono,我们开始不断崩溃,如下所示.

研究导致了这篇文章http://www.aaronheise.com/2012/12/monotouch-sqlite-sigsegv/

他正在使用Mono.Data.sqliteClient,而不是像我们一样使用sqlite.net.

他的解决方案涉及显式处理Command对象以确保GC可以保持等等.当我尝试将我的Command对象(来自sqlite.net)包装在using(){}子句中时,我发现它们不是一次性的.

我尝试插入100毫秒延迟并停止崩溃,但对我们来说这不是一个可行的解决方案.

这里对sqlite.net有什么希望,还是应该寻找一种不同的方式来使用sqlite?

    mono-rt: Stacktrace:


mono-rt:   at <unkNown> <0xffffffff>

mono-rt:   at (wrapper managed-to-native) sqlite.sqlite3.Prepare2 (intptr,string,int,intptr&,intptr) <IL 0x0003c, 0xffffffff>

...

mono-rt: 
Native stacktrace:


mono-rt: 
Got a SIGSEGV while executing native code. This usually indicates
a Fatal error in the mono runtime or one of the native libraries 
used by your application.

解决方法:

当我尝试从多个线程敲击相同的sqlite.net连接时,我很确定我得到了有意义的错误而不是SIGSEGV,但是如果你认为那是罪魁祸首,解决方案很简单:你需要限制对任何sqlite的访问.将数据库一次触摸到一个线程的网络方法.

在您在应用程序中共享单个sqliteConnection实例的场景中(这是一种非常有效的处理方式),我建议创建一个简化的代理类来包装您的sqlite.net连接,只公开您想要的方法并保护访问具有锁定语句的那些,即:

public class DatabaseWrapper : Idisposable
{
    // Fields.
    private readonly sqliteConnection Connection;
    private readonly object Lock = new object();

    public DatabaseWrapper(string databasePath)
    {
        if (string.IsNullOrEmpty(databasePath)) throw new ArgumentException("Database path cannot be null or empty.");

        this.Connection = new sqliteConnection(databasePath);
    }

    public IEnumerable<T> Entities<T>() where T : new()
    {
        lock (this.Lock)
        {
            return this.Connection.Table<T>();
        }
    }

    public IEnumerable<T> Query<T>(string query, params object[] args) where T : new()
    {
        lock (this.Lock)
        {
            return this.Connection.Query<T>(query, args);
        }
    }

    public int ExecuteNonQuery(string sql, params object[] args)
    {
        lock (this.Lock)
        {
            return this.Connection.Execute(sql, args);
        }
    }

    public T ExecuteScalar<T>(string sql, params object[] args)
    {
        lock (this.Lock)
        {
            return this.Connection.ExecuteScalar<T>(sql, args);
        }
    }

    public void Insert<T>(T entity)
    {
        lock (this.Lock)
        {
            this.Connection.Insert(entity);
        }
    }

    public void Update<T>(T entity)
    {
        lock (this.Lock)
        {
            this.Connection.Update(entity);
        }
    }

    public void Upsert<T>(T entity)
    {
        lock (this.Lock)
        {
            var rowCount = this.Connection.Update(entity);

            if (rowCount == 0)
            {
                this.Connection.Insert(entity);
            }
        }
    }

    public void Delete<T>(T entity)
    {
        lock (this.Lock)
        {
            this.Connection.Delete(entity);
        }
    }

    public void dispose()
    {
        this.Connection.dispose();
    }
}

附:显然,因为你在多个线程上做事,你需要非常小心不要引入竞争条件,这就是为什么,例如,我包含了保证以原子方式执行两步“更新或插入”操作的Upsert方法.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能