Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Offline Persistence in Firestore in a Flutter App

Tags:

I'm developing a app that uses Firebase's Firestore to send data to the web. One of the functions of the app is being able to save data in the device while being offline and send it to Firestore when internet connection is restored. I activated offline persistence but it dosen't work.

DEBUG CONSOLE:

W/OkHttpClientTransport(28536): Failed closing connection
W/OkHttpClientTransport(28536): javax.net.ssl.SSLException: Write error: ssl=0x7f7acfc408: I/O error during system call, Broken pipe
W/OkHttpClientTransport(28536):     at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_write(Native Method)
W/OkHttpClientTransport(28536):     at com.google.android.gms.org.conscrypt.NativeSsl.write(:com.google.android.gms@[email protected] (040406-222931072):4)

How can I activate offline persistence and sync with Firestore when internet is restored?

MY CODE:

Future<Null> sendFirebaseData(var selectedModel) async {

    Firestore.instance.enablePersistence(true);
    var certID = await getIDCertificado();

    var dateTime = new DateTime.now();
    var nowHour = new DateFormat('kk:mm:ss').format(dateTime);

      Map<String, dynamic> dataHeader = {
        'ID':                certID,
        };

      Map<String, dynamic> finalDataMap = {}..addAll(dataGeneral)
                                          ..addAll(dataInstrumento)..addAll(dataPadrao)
                                          ..addAll(dataAdicional)..addAll(dataHeader);

      await Firestore.instance.collection('certificados').document((certID.toString()))
          .setData(finalDataMap);}
like image 743
Brenno Fagundes Avatar asked Nov 30 '18 00:11

Brenno Fagundes


2 Answers

when you use offline persistence in Firestore, don't use Transactions or await for response.

so, change this :

  await Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

to this:

 Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

When you restore your internet connection your data will be sync automatically, even if you are in background.

Doesn't work when your app is closed.

Context Of Promises & Callbacks when Offline

Why the above code change to remove "await" works.

Reference: Firebase Video - How Do I Enable Offline Support 11:13

Your callback won't be called and your promise won't complete until the document write has been successful on the server. This is why if your UI waits until the write completes to do something, it appears to freeze in "offline mode" even if the write was actually made to the local cache.

It is OK to not use async / await, .then() or callbacks. Firestore will always "act" as if the data change was applied immediately, so you don't need to wait to be working with fresh data.

You only need to use callbacks and promises when you need to be sure that a server write has happened, and you want to block other things from happening until you get that confirmation.

like image 75
diegoveloper Avatar answered Sep 23 '22 15:09

diegoveloper


I think the currrent answer is outdated. According to the firebase documentation, offline persistentence is enabled by default for Android and iOS. For the web, it is not.

In flutter, the firestore implementation is based on the underlying OS. So you're safe on mobile apps, but not with flutter for web.

like image 42
yvan vander sanden Avatar answered Sep 23 '22 15:09

yvan vander sanden