1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-22 11:35:21 +01:00

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 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);

View File

@ -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<JsonItem> _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<JsonItem> GetCollection(LiteDatabase db)
{
return db?.GetCollection<JsonItem>("json_items");
}
public Task<T> GetAsync<T>(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<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
{
_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();
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

View File

@ -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);