Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore add method does not work properly when I am offline

If I run the following line of code when my device is offline, The card will be added to local cache. But the application never goes to the next line.

const data = await firebase.firestore().collection('cards').add(card);
console.log(data) // this line never get executed!
like image 725
a.toraby Avatar asked Dec 31 '22 22:12

a.toraby


1 Answers

Write methods (such as add()) return a promise that fulfills once the write operation has been completed (or rejected) on the server. Since you're not connected to the server, that never happens and thus your await will stay blocked.

Note that you don't need to await for the local write operation, as the write to the local cache happens synchronously. So as soon as the add(...) call returns (and doesn't raise an exception), you can be sure that the write was completed to the local cache. Hence, you should only use await if you need to know that the write was also handled by the server, in which case the behavior you see is exactly what you'll want.

like image 163
Frank van Puffelen Avatar answered Jan 13 '23 13:01

Frank van Puffelen