openedAfter method
This persistence, with its store opened only once previous completes, or after timeout
(5 s) when it does not; an error of previous does not stop the open either. The cache size
and what is cached are those of this persistence.
For a client that replaces another one on the same app name and database URL: pass the
close() of the old client, so that the new client opens the store after the old one released
it, instead of finding it in use (then sqlitePersistence waits about 7.5 s for the file, or
keeps the data in memory when the same persistence object opened it, or when the file stays
held longer than that wait). Calls made meanwhile wait, as they wait for any open. When
previous has not completed within timeout, the store's own rules decide as without this
method. In the worst case (the old close hangs) the new store opens after this 5 s plus
the store's own 7.5 s wait for a held file.
It changes when the new client takes the store, not what the store holds: the writes the old
client had not had acknowledged stay in the store and are sent again by the new client once it
owns the store, with the token of the new client's app, although their callers got
database-closed. That is the store's behaviour across any close and reopen (as across a
restart); this method only makes it happen at the new client's open, rather than after a
7.5 s wait or, when the store kept its data in memory, not until a later start.
The store is keyed by app name and database URL, not by user: when the signed-in user
differs between the old and the new client, the new client sends the old user's writes with
its own user's token, and the security rules evaluate them as that user. The official Android
and iOS SDKs behave the same. Use a directory per user (the uid) for the store, or clear the
old user's store (clearSqlitePersistence in firebase_database_dart_sqlite, which drops
that user's unsent writes) before the new client opens.
Implementation
DatabasePersistence openedAfter(
Future<void> previous, {
Duration timeout = const Duration(seconds: 5),
}) {
final PersistenceStorageEngineOpener opener = _opener;
return DatabasePersistence._(
(PersistenceKey key, {DatabaseLogger? logger}) async {
try {
await previous.timeout(timeout);
} on Object {
// Documented: the open goes ahead after a failed or late previous close; the store's
// own ownership rules then decide.
}
return opener(key, logger: logger);
},
_cacheSizeBytes,
_cacheServerData,
);
}