Skip to content

Commit 40fd290

Browse files
authored
fix(database): fix a regression with database localEvents handling (#18257)
* fix(database): fix a regression with database localEvents handling * fix iOS
1 parent 851a1fe commit 40fd290

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

packages/firebase_database/firebase_database/android/src/main/kotlin/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ class FirebaseDatabasePlugin :
770770
callback(KotlinResult.success(Unit))
771771
}
772772
}
773-
})
773+
}, request.applyLocally)
774774
} catch (e: Exception) {
775775
// Convert generic exceptions to FlutterFirebaseDatabaseException for proper error handling
776776
val flutterException = if (e is FlutterFirebaseDatabaseException) e else FlutterFirebaseDatabaseException.unknown(e.message ?: "Unknown transaction error")

packages/firebase_database/firebase_database/ios/firebase_database/Sources/firebase_database/FLTFirebaseDatabasePlugin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public class FLTFirebaseDatabasePlugin: NSObject, FlutterPlugin, FLTFirebasePlug
251251
let database = getDatabaseFromPigeonApp(app)
252252
let reference = database.reference(withPath: request.path)
253253

254-
reference.runTransactionBlock { currentData in
254+
reference.runTransactionBlock({ currentData in
255255
let semaphore = DispatchSemaphore(value: 0)
256256
var transactionResult: TransactionHandlerResult?
257257

@@ -284,7 +284,7 @@ public class FLTFirebaseDatabasePlugin: NSObject, FlutterPlugin, FLTFirebasePlug
284284

285285
currentData.value = result.value
286286
return TransactionResult.success(withValue: currentData)
287-
} andCompletionBlock: { error, committed, snapshot in
287+
}, andCompletionBlock: { error, committed, snapshot in
288288
if let error {
289289
completion(.failure(self.createFlutterError(error)))
290290
return
@@ -304,7 +304,7 @@ public class FLTFirebaseDatabasePlugin: NSObject, FlutterPlugin, FLTFirebasePlug
304304
]
305305

306306
completion(.success(()))
307-
}
307+
}, withLocalEvents: request.applyLocally)
308308
}
309309

310310
func databaseReferenceGetTransactionResult(app: DatabasePigeonFirebaseApp, transactionKey: Int64,

tests/integration_test/firebase_database/database_reference_e2e.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,41 @@ void setupDatabaseReferenceTests() {
126126
expect(result.snapshot.value, 5);
127127
});
128128

129+
test('does not emit local transaction events when disabled', () async {
130+
final ref = database.ref('tests/transaction-apply-locally-false');
131+
await ref.set({'count': 0});
132+
133+
final initialEvent = Completer<void>();
134+
final events = <Object?>[];
135+
final subscription = ref.onValue.listen((event) {
136+
if (!initialEvent.isCompleted) {
137+
initialEvent.complete();
138+
return;
139+
}
140+
141+
events.add(event.snapshot.value);
142+
});
143+
144+
try {
145+
await initialEvent.future.timeout(const Duration(seconds: 5));
146+
147+
await ref.runTransaction(
148+
(value) => Transaction.success({
149+
'count': ((value as Map?)?['count'] as int? ?? 0) + 1,
150+
'timestamp': ServerValue.timestamp,
151+
}),
152+
applyLocally: false,
153+
);
154+
155+
await Future<void>.delayed(const Duration(seconds: 1));
156+
157+
expect(events, hasLength(1));
158+
} finally {
159+
await database.goOnline();
160+
await subscription.cancel();
161+
}
162+
});
163+
129164
test('executes transaction', () async {
130165
final ref = database.ref('tests/transaction-exec');
131166
await ref.set(0);

0 commit comments

Comments
 (0)