mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 10:28:28 +03: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:
parent
acf2e4360f
commit
ef4b53b337
3 changed files with 65 additions and 41 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
return new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
|
||||
}
|
||||
if (_initTask != null)
|
||||
|
||||
private ILiteCollection<JsonItem> GetCollection(LiteDatabase db)
|
||||
{
|
||||
return _initTask;
|
||||
return db?.GetCollection<JsonItem>("json_items");
|
||||
}
|
||||
_initTask = Task.Run(() =>
|
||||
{
|
||||
try
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_db == null)
|
||||
LiteDatabase db = null;
|
||||
try
|
||||
{
|
||||
_db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
|
||||
db = GetDb();
|
||||
var collection = GetCollection(db);
|
||||
if (db == null || collection == null)
|
||||
{
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
var item = collection.Find(i => i.Id == key).FirstOrDefault();
|
||||
if (item == null)
|
||||
{
|
||||
return Task.FromResult(default(T));
|
||||
}
|
||||
_collection = _db.GetCollection<JsonItem>("json_items");
|
||||
return Task.FromResult(JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_initTask = null;
|
||||
db?.Dispose();
|
||||
}
|
||||
}
|
||||
});
|
||||
return _initTask;
|
||||
}
|
||||
|
||||
public async Task<T> GetAsync<T>(string key)
|
||||
public Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
await InitAsync();
|
||||
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
|
||||
if (item == null)
|
||||
lock (_lock)
|
||||
{
|
||||
return default(T);
|
||||
LiteDatabase db = null;
|
||||
try
|
||||
{
|
||||
db = GetDb();
|
||||
var collection = GetCollection(db);
|
||||
if (db == null || collection == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
return JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings);
|
||||
}
|
||||
|
||||
public async Task SaveAsync<T>(string key, T obj)
|
||||
{
|
||||
await InitAsync();
|
||||
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
|
||||
_collection.Upsert(new JsonItem(key, data));
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue