Fix ObjectBoxCache singleton to return existing cache object if already existing for path

codemagic-setup
Hank Grabowski 2023-03-22 12:45:39 -04:00
rodzic 48a6a404d6
commit 951f50ee7a
1 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -4,19 +4,29 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import '../../globals.dart';
import '../../objectbox.g.dart';
class ObjectBoxCache {
static final _logger = Logger('ObjectBoxCache');
static final _openCaches = <String, ObjectBoxCache>{};
late final Store store;
ObjectBoxCache._create(this.store);
static Future<ObjectBoxCache> create(
{String baseDir = 'objectboxcache', String? subDir}) async {
static Future<ObjectBoxCache> create({
String baseDir = 'objectboxcache',
String? subDir,
}) async {
final docsDir = await getApplicationSupportDirectory();
final path = p.join(docsDir.path, baseDir, subDir);
_logger.info('ObjectBoxCache path: $path');
if (_openCaches.containsKey(path)) {
_logger.fine('Cache already exists for path, returning it: $path');
return _openCaches[path]!;
}
try {
final directory = Directory(path);
if (!directory.existsSync()) {
@ -25,9 +35,14 @@ class ObjectBoxCache {
} catch (e) {
_logger.severe('Error creating ObjectCachePathDirectory: $e');
}
_logger.info('ObjectBoxCache path: $path');
final store = await openStore(
directory: path, macosApplicationGroup: 'T69YZGT58U.relatica');
return ObjectBoxCache._create(store);
directory: path,
macosApplicationGroup: macOsGroupId,
);
final cache = ObjectBoxCache._create(store);
_openCaches[path] = cache;
_logger.fine('New cache created for path: $path');
return cache;
}
}