From acda93f8e1b1446fb15133ccad4ee1c96c9323c9 Mon Sep 17 00:00:00 2001 From: dab246 Date: Wed, 24 May 2023 09:40:28 +0700 Subject: [PATCH] TF-1869 Reopen box when update cache on background (cherry picked from commit bf2dd9664cc9d319e3ce9d0fe02374cbe7c586f2) --- .../caching/config/hive_cache_client.dart | 22 ++++++++++++++++--- .../data/local/email_cache_manager.dart | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/features/caching/config/hive_cache_client.dart b/lib/features/caching/config/hive_cache_client.dart index f63eb05c2..1311c7274 100644 --- a/lib/features/caching/config/hive_cache_client.dart +++ b/lib/features/caching/config/hive_cache_client.dart @@ -57,8 +57,11 @@ abstract class HiveCacheClient { }); } - Future getItem(String key) { + Future getItem(String key, {bool needToReopen = false}) { return Future.sync(() async { + if (needToReopen) { + await closeBox(); + } final boxItem = encryption ? await openBoxEncryption() : await openBox(); @@ -68,8 +71,11 @@ abstract class HiveCacheClient { }); } - Future> getAll() { + Future> getAll({bool needToReopen = false}) { return Future.sync(() async { + if (needToReopen) { + await closeBox(); + } final boxItem = encryption ? await openBoxEncryption() : await openBox(); @@ -79,8 +85,11 @@ abstract class HiveCacheClient { }); } - Future> getListByTupleKey(String accountId, String userName) { + Future> getListByTupleKey(String accountId, String userName, {bool needToReopen = false}) { return Future.sync(() async { + if (needToReopen) { + await closeBox(); + } final boxItem = encryption ? await openBoxEncryption() : await openBox(); return boxItem.toMap() .where((key, value) => _matchedKey(key, accountId, userName)) @@ -174,4 +183,11 @@ abstract class HiveCacheClient { throw error; }); } + + Future closeBox() async { + if (Hive.isBoxOpen(tableName)) { + await Hive.box(tableName).close(); + } + return Future.value(); + } } \ No newline at end of file diff --git a/lib/features/thread/data/local/email_cache_manager.dart b/lib/features/thread/data/local/email_cache_manager.dart index f1242e990..34d7a72a4 100644 --- a/lib/features/thread/data/local/email_cache_manager.dart +++ b/lib/features/thread/data/local/email_cache_manager.dart @@ -99,6 +99,6 @@ class EmailCacheManager { Future getEmailFromCache(AccountId accountId, UserName userName, EmailId emailId) { final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey; - return _emailCacheClient.getItem(keyCache); + return _emailCacheClient.getItem(keyCache, needToReopen: true); } } \ No newline at end of file