Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore access broken after an offline attempt

I'm trying to access to a Cloud Firestore with this piece of code :

void _submit(BuildContext context) async {
    final DocumentReference postRef = Firestore.instance.document(dbPath);
    Firestore.instance.runTransaction((transaction) async {
        DocumentSnapshot freshSnap = await transaction.get(postRef);
        await transaction.update(freshSnap.reference, {
            'value': freshSnap['value'] + 1
        });
   });
}

If wifi or mobile data are on, everything works fine. (as expected)

If wifi and mobile data are off, it does not work. (as expected). But when I wait until the timeout (after calling the method) and only then, turn mobile data and wifi on, it does not work anymore and I get the following errors :

E/flutter ( 7041): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7041): PlatformException(Error performing transaction, Timed out waiting for Task, null)
E/flutter ( 7041): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:547:7)
E/flutter ( 7041): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #2      Firestore.runTransaction (file:///C:/{myPath}/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.7.3/lib/src/firestore.dart:115:10)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #3      _FeedbackPageState._submitFeedback (package:appli_salon_data/view/program/FeedbackPage.dart:74:26)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #4      _FeedbackPageState.build.<anonymous closure> (package:appli_salon_data/view/program/FeedbackPage.dart:60:26)
E/flutter ( 7041): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
E/flutter ( 7041): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
E/flutter ( 7041): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 7041): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter ( 7041): #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
E/flutter ( 7041): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7041): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter ( 7041): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 7041): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 7041): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 7041): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 7041): #16     _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter ( 7041): #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)

(not expected)

The method won't work again until I relaunch the app.

If anyone has some explanation about this behaviour, feel free to answer :)

UPDATE : I tried this :

  1. Switch off Wifi
  2. Try to run the transaction - results in expected failure
  3. Switch on Wifi
  4. Try to run the transaction

Here is the interesting part : at step 4, the transaction is "immediately" run twice, both times getting the error :

PlatformException(Error performing Transaction#get, UNAVAILABLE: Unable to resolve host firestore.googleapis.com, null)

when calling transaction.get(postRef).

Could it mean that Firestore somehow loses all access to the host after losing Internet connection briefly once ? How can I fix that ?

like image 898
DeepProblems Avatar asked Jun 27 '18 10:06

DeepProblems


People also ask

Does firebase firestore work offline?

Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data.

Is firebase persistent?

Note that Firebase Auth web sessions are single host origin and will be persisted for a single domain only. Indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed.

Does firebase require Internet?

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

Can't reach cloud firestore backend react?

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.


1 Answers

Have you tried enabling local persistence? Not sure if that'll catch the issue, but it might be worth a try. I believe this is done using the persistenceEnabled parameter in the settings method:

Firestore.instance.settings(persistenceEnabled: true)

Keep in mind that this should only be done once, so it should go somewhere in your code that won't get called every time you access any Firestore data.

like image 90
JJ Geewax Avatar answered Sep 20 '22 04:09

JJ Geewax