sqlite在锁定或异步时的正确用法是什么 [英] What is the correct usage for sqlite on locking or async

查看:252
本文介绍了sqlite在锁定或异步时的正确用法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Xamarin通过SQLite为Android和ios编写C#代码.但是,关于如何使用sqlite,我似乎有一个概念上的误解:

We are using Xamarin to write C# code with SQLite for android and ios. However about how to use sqlite, I seem to have a conceptual misunderstanding:

在Android上使用SQLite的最佳做法是什么?

根据stackoverflow回答,它说-一个帮助程序和一个数据库连接.在其周围使用锁定,以确保任何时候任何一个线程都在访问sqlite db.

According to the stackoverflow answer it says - one helper and one db connection. Use lock around it to make sure only one thread is accessing sqlite db at any time.

我的问题是-如果是这样-异步有什么用?

My question is - if that is the case - what is the use for async?

我试图将异步代码与同步代码一起使用-该代码正确地给了我编译错误,以避免死锁.
为什么不能我在lock语句的主体中使用了'await'运算符?

I tried to use async with synchronized code - and the code gave me compile errors rightfully to avoid deadlocks.
Why can't I use the 'await' operator within the body of a lock statement?

    public async Task InsertAsync<T> (T item){
        lock (mutex) {
            await asyncConnection.InsertAsync (item);
        }
    }

    public async Task InsertOrUpdateAsync<T> (T item){
        lock (mutex) {
            int count = await asyncConnection.UpdateAsync (item);
            if (0 == count) {
                await asyncConnection.InsertAsync (item);
            }
        }
    }

失败.但是-如果我要使用锁来确保我一次在一个线程中使用一个连接一个线程,这是sqlite中的最佳做法-为什么根本没有一个异步sqlite库?

fails. However - if I am going to use locks to ensure I am using one connection one thread at a time as a best practice in sqlite - why is there an async sqlite library at all?

为什么有些线程在网络上推广异步sqlite使用.

And why are some threads promoting async sqlite usage on the web.

android和iphone中sqlite的真正最佳实践是什么?只是使用同步版本?

What is the real best practice for sqlite in android and iphone? Just using sync version?

推荐答案

同步与异步单与并发之间有很大的区别,并且共有4种组合他们.

There's a big difference between synchronous vs asynchronous and single vs concurrent and there are all 4 combinations of them.

异步的情况下,您最多只能使用一个线程访问数据库,但是在整个操作过程中以及何时不需使用同一线程您不需要线程(在等待IO操作完成时),根本不需要任何线程.

In the single asynchronous case you access the DB using a single thread at most, but it doesn't need to be the same thread throughout the operation, and when you don't need a thread (when you are waiting for the IO operation to complete) you don't need any threads at all.

一次将async使用限制为单个操作的最基本方法是将SemaphoreSliminitialCount = 1一起使用.更好的方法是使用AsyncLock(构建异步协调基元,第6部分:Stephen Toub的AsyncLock ):

The most basic way to limit the async usage to a single operation at a time is by using a SemaphoreSlim with initialCount = 1. A nicer way would be to use an AsyncLock (Building Async Coordination Primitives, Part 6: AsyncLock by Stephen Toub):

private readonly AsyncLock _lock = new AsyncLock(); 

public async Task InsertAsync<T> (T item)
{
    using(await _lock.LockAsync())
    {
        await asyncConnection.InsertAsync (item);
    }
}

public async Task InsertOrUpdateAsync<T> (T item)
{
    using(await _lock.LockAsync())
    {
        if (0 == await asyncConnection.UpdateAsync (item))
        {
            await asyncConnection.InsertAsync (item);
        }
    }
}

注意:我对AsyncLock

这篇关于sqlite在锁定或异步时的正确用法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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