From ef4b53b33759ff85cf09bc7ce88be9369bc9344f Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Wed, 16 Dec 2020 16:37:26 -0500 Subject: [PATCH] Workaround for lack of shared DB support (#1182) * workaround for lack of shared DB support * dispose db in finally --- src/Android/MainApplication.cs | 1 - src/Core/Services/LiteDbStorageService.cs | 104 ++++++++++++++-------- src/iOS.Core/Utilities/iOSCoreHelpers.cs | 1 - 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index 3cc9ea12a..1962019ab 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -87,7 +87,6 @@ namespace Bit.Droid var preferencesStorage = new PreferencesStorageService(null); var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db")); - liteDbStorage.InitAsync(); var localizeService = new LocalizeService(); var broadcasterService = new BroadcasterService(); var messagingService = new MobileBroadcasterMessagingService(broadcasterService); diff --git a/src/Core/Services/LiteDbStorageService.cs b/src/Core/Services/LiteDbStorageService.cs index 9fcdcb3a7..35a450536 100644 --- a/src/Core/Services/LiteDbStorageService.cs +++ b/src/Core/Services/LiteDbStorageService.cs @@ -9,7 +9,6 @@ namespace Bit.Core.Services { public class LiteDbStorageService : IStorageService { - private static LiteDatabase _db; private static readonly object _lock = new object(); private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings @@ -17,67 +16,94 @@ namespace Bit.Core.Services ContractResolver = new CamelCasePropertyNamesContractResolver() }; private readonly string _dbPath; - private ILiteCollection _collection; - private Task _initTask; public LiteDbStorageService(string dbPath) { _dbPath = dbPath; } - public Task InitAsync() + private LiteDatabase GetDb() { - if (_collection != null) - { - return Task.FromResult(0); - } - if (_initTask != null) - { - return _initTask; - } - _initTask = Task.Run(() => + return new LiteDatabase($"Filename={_dbPath};Upgrade=true;"); + } + + private ILiteCollection GetCollection(LiteDatabase db) + { + return db?.GetCollection("json_items"); + } + + public Task GetAsync(string key) + { + lock (_lock) { + LiteDatabase db = null; try { - lock (_lock) + db = GetDb(); + var collection = GetCollection(db); + if (db == null || collection == null) { - if (_db == null) - { - _db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;"); - } + return Task.FromResult(default(T)); } - _collection = _db.GetCollection("json_items"); + var item = collection.Find(i => i.Id == key).FirstOrDefault(); + if (item == null) + { + return Task.FromResult(default(T)); + } + return Task.FromResult(JsonConvert.DeserializeObject(item.Value, _jsonSettings)); } finally { - _initTask = null; + db?.Dispose(); } - }); - return _initTask; - } - - public async Task GetAsync(string key) - { - await InitAsync(); - var item = _collection.Find(i => i.Id == key).FirstOrDefault(); - if (item == null) - { - return default(T); } - return JsonConvert.DeserializeObject(item.Value, _jsonSettings); } - public async Task SaveAsync(string key, T obj) + public Task SaveAsync(string key, T obj) { - await InitAsync(); - var data = JsonConvert.SerializeObject(obj, _jsonSettings); - _collection.Upsert(new JsonItem(key, data)); + lock (_lock) + { + LiteDatabase db = null; + try + { + db = GetDb(); + var collection = GetCollection(db); + if (db == null || collection == null) + { + return Task.CompletedTask; + } + var data = JsonConvert.SerializeObject(obj, _jsonSettings); + collection.Upsert(new JsonItem(key, data)); + return Task.CompletedTask; + } + finally + { + db?.Dispose(); + } + } } - public async Task RemoveAsync(string key) + public Task RemoveAsync(string key) { - await InitAsync(); - _collection.DeleteMany(i => i.Id == key); + lock (_lock) + { + LiteDatabase db = null; + try + { + db = GetDb(); + var collection = GetCollection(db); + if (db == null || collection == null) + { + return Task.CompletedTask; + } + collection.DeleteMany(i => i.Id == key); + return Task.CompletedTask; + } + finally + { + db?.Dispose(); + } + } } private class JsonItem diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index 47acc2d83..8bd49db03 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -43,7 +43,6 @@ namespace Bit.iOS.Core.Utilities var appGroupContainer = new NSFileManager().GetContainerUrl(AppGroupId); var liteDbStorage = new LiteDbStorageService( Path.Combine(appGroupContainer.Path, "Library", "bitwarden.db")); - liteDbStorage.InitAsync(); var localizeService = new LocalizeService(); var broadcasterService = new BroadcasterService(); var messagingService = new MobileBroadcasterMessagingService(broadcasterService);