diff --git a/lib/src/client.dart b/lib/src/client.dart index 8ef4001..2b6ab05 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -971,7 +971,7 @@ class Client { Future _syncRequest; Future _sync() async { - if (isLogged() == false) return; + if (isLogged() == false || _disposed) return; var action = '/client/r0/sync?filter=$syncFilters'; @@ -981,11 +981,13 @@ class Client { } try { _syncRequest = jsonRequest(type: HTTPType.GET, action: action); + if (_disposed) return; final hash = _syncRequest.hashCode; final syncResp = await _syncRequest; if (hash != _syncRequest.hashCode) return; _timeoutFactor = 1; final futures = handleSync(syncResp); + if (_disposed) return; await database?.transaction(() async { for (final f in futures) { await f(); @@ -2174,4 +2176,15 @@ class Client { rethrow; } } + + bool _disposed = false; + + /// Stops the synchronization and closes the database. After this + /// you can safely make this Client instance null. + Future dispose() async { + _disposed = true; + await database?.close(); + database = null; + return; + } }