Skip to content

🐛 [firebase_database] The 'plugins.flutter.io/firebase_database' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. While using runTransaction #11628

Description

@MirceaX2Mobile

Bug report

Describe the bug
While using firebase_database on iOS 17 (Xcode15) on runTransaction on real time database I'm getting this warning in console:

[VERBOSE-2:shell.cc(1004)] The 'plugins.flutter.io/firebase_database' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

Steps to reproduce

Steps to reproduce the behaviour:

  1. Add the latest pub firebase_database
    firebase_database: ^10.2.6

  2. Run a simple transaction request to the firebase database

  final dbRef = FirebaseDatabase.instance.ref();
  updateAnalytics() {
    dbRef
        .child("analytics")
        .child("number")
        .runTransaction((Object? oldValue) {
        if (oldValue == null) {
          return Transaction.success(1);
        }
        int _newValue = (oldValue as int? ?? 0) + 1;
        return Transaction.success(_newValue);
    });
  }

Expected behavior

Use the plugin without getting warnings in the console.

Additional context

Run using iPhone 15 Pro iOS 17
Xcode 15
Flutter 3.13.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 12fccda598 (4 days ago) • 2023-09-19 13:56:11 -0700
Engine • revision bd986c5ed2
Tools • Dart 3.1.2 • DevTools 2.25.0

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.5, on macOS 13.5.2 22G91 darwin-arm64, locale
    en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Android Studio (version 2022.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

Solution

I found a fix. The error is triggered because channel handlers are not executed on background threads. If you go the FLTFirebaseDatabasePlugin file inside the method - (void)databaseRunTransaction:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
the cannel handler is not running on the main thread.

        [strongSelf->_channel invokeMethod:@"FirebaseDatabase#callTransactionHandler"
                                 arguments:@{
                                   @"transactionKey" : @(transactionKey),
                                   @"snapshot" : @{
                                     @"key" : currentData.key ?: [NSNull null],
                                     @"value" : currentData.value ?: [NSNull null],
                                   }
                                 }
                                    result:methodCallResultHandler];

In order to fix this we need to add dispatch_async and the warning is not shown anymore.

      dispatch_async(dispatch_get_main_queue(), ^{
          [strongSelf->_channel invokeMethod:@"FirebaseDatabase#callTransactionHandler"
                                   arguments:@{
                                     @"transactionKey" : @(transactionKey),
                                     @"snapshot" : @{
                                       @"key" : currentData.key ?: [NSNull null],
                                       @"value" : currentData.value ?: [NSNull null],
                                     }
                                   }
                                      result:methodCallResultHandler];
      });

Metadata

Metadata

Labels

platform: iosIssues / PRs which are specifically for iOS.plugin: databaseresolution: fixedA fix has been merged or is pending merge from a PR.type: bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions