簡體   English   中英

數據庫被鎖定:在我的 android 和 ios xamarin 應用程序上拋出了 SQLite 異常

[英]Database is locked: SQLite exception thrown on my android and ios xamarin app

數據庫非常穩定,直到我完成了所有的包和包簽名並將應用程序部署到應用程序中心進行分布式測試,然后我開始從 SQLite 數據庫中獲取各種錯誤。 我做了一些研究並了解到數據庫不是線程安全的,但是我如何使它線程安全,我所有的數據庫連接和查詢都是通過異步進行的。
這是我的數據庫連接

    private static object collisionLock = new object();
    //local SQLite database.

    public SQLiteAsyncConnection Db;
    private Uri url;
    private MemberDataStore membersStore;
    private AdvantageMemberDataStore advMembersStore;
    private EventDataStore eventDataStore;



    public ConnectionsUtil()
    {
         Db = DependencyService.Get<IDatabaseConnection>().DbConnection();
    }       

這是執行插入和更新的代碼,我有三種方法,如下所示訪問數據庫和許多使用應用程序中的 Db 的查詢

    public async Task GetAdvantageMembersAsync(AdvantageMemberDataStore store)
    {
        await Db.QueryAsync<AdvantageMember>("DROP TABLE IF EXISTS AdvantageMember").ContinueWith(t =>
        {

            Db.CreateTableAsync<AdvantageMember>();

            Console.WriteLine("AdvantageMember table created!");

        });
        url = new Uri("http://54.39.180.209/benefits");

        Console.WriteLine("Gideon: Attempting to get advantage members.");
        try
        {

            string json;
            //Gideon: get json string containing advantage members.
            using (WebClient sr = new WebClient())
            {

                Console.WriteLine("Gideon: Retrieving advantage Members....");

                sr.DownloadStringCompleted += async (s, e) =>
                {

                    Console.WriteLine("Gideon: Advantage Members Downloaded. Processing advantage Members....");
                    json = e.Result;
                    lock (collisionLock)
                    {
                       Db.InsertAllAsync(JsonConvert.DeserializeObject<IEnumerable<AdvantageMember>>(json)).ContinueWith(async t =>
                        {
                            await store.UpdateItemsAsync(Db.QueryAsync<AdvantageMember>("SELECT * FROM AdvantageMember GROUP BY Title").Result);
                        });
                    }

                        Console.WriteLine("Processing Members benefits for android");
                        advMembersStore = store;      
                };

                await Task.Run(() => sr.DownloadStringAsync(url));

            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Gideon: An error occured while trying to dbect. ERROR: " + e);
        }
    }
 **This is the code that specify the folder for the sqlite connection**

 public class DatabaseConnection_iOS: Services.IDatabaseConnection
{
    public SQLiteAsyncConnection DbConnection()
    {
        var dbName = "MyDb.db3";
        string personalFolder =
          Environment.
          GetFolderPath(Environment.SpecialFolder.Personal);
        string libraryFolder =
          Path.Combine(personalFolder, "..", "Library");
        var path = Path.Combine(libraryFolder, dbName);
        return new SQLiteAsyncConnection(path);
    }
}

就我而言,我將 SaveContext 放在 foreach 中

            var transaction = _context.Database.BeginTransaction();

            try
            {
                foreach (var item in obj)
                {
                    _context.EntrevistaNacionals.Add(item);
await _context.SaveChangesAsync(); //--wrong 
                }
                
                transaction.Commit();

                return true;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM