Workaround for lack of shared DB support (#1182)

* workaround for lack of shared DB support

* dispose db in finally
This commit is contained in:
Matt Portune 2020-12-16 16:37:26 -05:00 committed by GitHub
parent acf2e4360f
commit ef4b53b337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 41 deletions

View File

@ -87,7 +87,6 @@ namespace Bit.Droid
var preferencesStorage = new PreferencesStorageService(null); var preferencesStorage = new PreferencesStorageService(null);
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db")); var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db"));
liteDbStorage.InitAsync();
var localizeService = new LocalizeService(); var localizeService = new LocalizeService();
var broadcasterService = new BroadcasterService(); var broadcasterService = new BroadcasterService();
var messagingService = new MobileBroadcasterMessagingService(broadcasterService); var messagingService = new MobileBroadcasterMessagingService(broadcasterService);

View File

@ -9,7 +9,6 @@ namespace Bit.Core.Services
{ {
public class LiteDbStorageService : IStorageService public class LiteDbStorageService : IStorageService
{ {
private static LiteDatabase _db;
private static readonly object _lock = new object(); private static readonly object _lock = new object();
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
@ -17,67 +16,94 @@ namespace Bit.Core.Services
ContractResolver = new CamelCasePropertyNamesContractResolver() ContractResolver = new CamelCasePropertyNamesContractResolver()
}; };
private readonly string _dbPath; private readonly string _dbPath;
private ILiteCollection<JsonItem> _collection;
private Task _initTask;
public LiteDbStorageService(string dbPath) public LiteDbStorageService(string dbPath)
{ {
_dbPath = dbPath; _dbPath = dbPath;
} }
public Task InitAsync() private LiteDatabase GetDb()
{ {
if (_collection != null) return new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
{ }
return Task.FromResult(0);
} private ILiteCollection<JsonItem> GetCollection(LiteDatabase db)
if (_initTask != null) {
{ return db?.GetCollection<JsonItem>("json_items");
return _initTask; }
}
_initTask = Task.Run(() => public Task<T> GetAsync<T>(string key)
{
lock (_lock)
{ {
LiteDatabase db = null;
try try
{ {
lock (_lock) db = GetDb();
var collection = GetCollection(db);
if (db == null || collection == null)
{ {
if (_db == null) return Task.FromResult(default(T));
{
_db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
}
} }
_collection = _db.GetCollection<JsonItem>("json_items"); var item = collection.Find(i => i.Id == key).FirstOrDefault();
if (item == null)
{
return Task.FromResult(default(T));
}
return Task.FromResult(JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings));
} }
finally finally
{ {
_initTask = null; db?.Dispose();
} }
});
return _initTask;
}
public async Task<T> GetAsync<T>(string key)
{
await InitAsync();
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
if (item == null)
{
return default(T);
} }
return JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings);
} }
public async Task SaveAsync<T>(string key, T obj) public Task SaveAsync<T>(string key, T obj)
{ {
await InitAsync(); lock (_lock)
var data = JsonConvert.SerializeObject(obj, _jsonSettings); {
_collection.Upsert(new JsonItem(key, data)); 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(); lock (_lock)
_collection.DeleteMany(i => i.Id == key); {
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 private class JsonItem

View File

@ -43,7 +43,6 @@ namespace Bit.iOS.Core.Utilities
var appGroupContainer = new NSFileManager().GetContainerUrl(AppGroupId); var appGroupContainer = new NSFileManager().GetContainerUrl(AppGroupId);
var liteDbStorage = new LiteDbStorageService( var liteDbStorage = new LiteDbStorageService(
Path.Combine(appGroupContainer.Path, "Library", "bitwarden.db")); Path.Combine(appGroupContainer.Path, "Library", "bitwarden.db"));
liteDbStorage.InitAsync();
var localizeService = new LocalizeService(); var localizeService = new LocalizeService();
var broadcasterService = new BroadcasterService(); var broadcasterService = new BroadcasterService();
var messagingService = new MobileBroadcasterMessagingService(broadcasterService); var messagingService = new MobileBroadcasterMessagingService(broadcasterService);