1
0
mirror of https://github.com/bitwarden/mobile.git synced 2025-01-28 22:11:33 +01:00

Dispost of LiteDatabase instance (#928)

This commit is contained in:
Kyle Spearrin 2020-05-28 15:44:27 -04:00 committed by GitHub
parent cd3585be58
commit 0b29c6e5a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -1,11 +1,12 @@
using Bit.Core; using Bit.Core;
using Bit.Core.Abstractions; using Bit.Core.Abstractions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Bit.App.Services namespace Bit.App.Services
{ {
public class MobileStorageService : IStorageService public class MobileStorageService : IStorageService, IDisposable
{ {
private readonly IStorageService _preferencesStorageService; private readonly IStorageService _preferencesStorageService;
private readonly IStorageService _liteDbStorageService; private readonly IStorageService _liteDbStorageService;
@ -88,5 +89,17 @@ namespace Bit.App.Services
} }
return _liteDbStorageService.RemoveAsync(key); return _liteDbStorageService.RemoveAsync(key);
} }
public void Dispose()
{
if (_liteDbStorageService is IDisposable disposableLiteDbService)
{
disposableLiteDbService.Dispose();
}
if (_preferencesStorageService is IDisposable disposablePrefService)
{
disposablePrefService.Dispose();
}
}
} }
} }

View File

@ -2,12 +2,13 @@
using LiteDB; using LiteDB;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
public class LiteDbStorageService : IStorageService public class LiteDbStorageService : IStorageService, IDisposable
{ {
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{ {
@ -16,6 +17,7 @@ namespace Bit.Core.Services
private readonly string _dbPath; private readonly string _dbPath;
private ILiteCollection<JsonItem> _collection; private ILiteCollection<JsonItem> _collection;
private Task _initTask; private Task _initTask;
private LiteDatabase _db;
public LiteDbStorageService(string dbPath) public LiteDbStorageService(string dbPath)
{ {
@ -36,8 +38,8 @@ namespace Bit.Core.Services
{ {
try try
{ {
var db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;"); _db = new LiteDatabase($"Filename={_dbPath};Upgrade=true;");
_collection = db.GetCollection<JsonItem>("json_items"); _collection = _db.GetCollection<JsonItem>("json_items");
} }
finally finally
{ {
@ -71,6 +73,11 @@ namespace Bit.Core.Services
_collection.DeleteMany(i => i.Id == key); _collection.DeleteMany(i => i.Id == key);
} }
public void Dispose()
{
_db?.Dispose();
}
private class JsonItem private class JsonItem
{ {
public JsonItem() { } public JsonItem() { }

View File

@ -113,6 +113,13 @@ namespace Bit.Core.Utilities
public static void Reset() public static void Reset()
{ {
foreach (var service in RegisteredServices)
{
if (service.Value != null && service.Value is IDisposable disposableService)
{
disposableService.Dispose();
}
}
Inited = false; Inited = false;
RegisteredServices.Clear(); RegisteredServices.Clear();
RegisteredServices = new Dictionary<string, object>(); RegisteredServices = new Dictionary<string, object>();