Working on an app using block programming on sketchware since I don't know anything about coding. My problem is, I want my app to disconnect when the internet is not available. I have put a command that if my app can access a certain data on firebase then that means it is online but if not then it is disconnected, but it seems that my app can still access the data on firebase even without internet because of the cache...
FIREBASE CONSOLE DATABASE RULES :
SOURCE CODE - onPageFinished :
BLOCK 1:
BLOCK 2:
I think this should work for your needs:
mDatabase.getReference().keepSynced(false);
FirebaseDatabase.getInstance().setPersistenceEnabled(false);
Further Reference
UPDATE : This is for Android Studio users.
Related
I'm trying to set up an app using the Fitbit SDK, which I want to use to pull data down from my firestore database to the companion app in the fitbit application. However I am encountering an issue. The code is returning the following errors:
[15:29:50] Companion: Uncaught (in promise) TypeError: Cannot read property 'getEntriesByType' of undefined
new dn at node_modules/#firebase/webchannel-wrapper/dist/index.js:66,585
new Gn at node_modules/#firebase/webchannel-wrapper/dist/index.js:82,64
new gr at node_modules/#firebase/webchannel-wrapper/dist/index.js:95,30
pr.g at node_modules/#firebase/webchannel-wrapper/dist/index.js:94,385
Ao.ho at node_modules/#firebase/firestore/dist/index.esm2017.js:12548,19
Oo.Ko at node_modules/#firebase/firestore/dist/index.esm2017.js:13025,9
Oo.Uo at node_modules/#firebase/firestore/dist/index.esm2017.js:12978,23
? at node_modules/#firebase/firestore/dist/index.esm2017.js:12968,13
[15:30:00] Companion: [2022-08-04T14:30:00.245Z]
#elixr firebase
/firestore: Firestore (9.9.1): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
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. (node_modules/#firebase/logger/dist/esm/index.esm2017.js:78,9).
Which I can't seem to find a fix for online. The added complexity is with the environment for developing the code for this app is somewhere between node.js and web 9, so when looking at the docs on the website I have to use a mixture of the two guides to try figure out how to get it to work. The additional awkard part about this is that firestore doesn't enable app check for something that's neither iOS, Android or Web, while this could technically pass as a web app, I would need to somehow register a website just to get the recaptcha key from to then enable app check. But would have no way to verify the user each time using app check.
Is this even possible to do, as there's no docs on either end of fitbit or firebase to help in this sort of situation. I had though of potentially using direct websockets but don't know the restrictions on that kind of data transfer.
Does anyone know a work around/ how to remedy this?
This is not a problematic question, it was asked because just wanted to know what's actually going on behind it, I can't find an answer in any documentation.
When i open the app without internet it shows the data from firebase, i try to restart then it also shows, i try to clear the cache but it shows the data like it shows from the local database, Why is this happening, is there a built-in local storage or something in Firebase Flutter?
Firestore SDKs cache any data that they've recently seen, as well as any pending writes from the local client that haven't been synchronized to the server yet. On mobile clients (iOS and Android) this cache is enabled by default, while on web you can enable it with an API call.
For more on this, see the Firebase documentation on accessing Firestore data while you're offline.
I came across a wonderful feature of Firebase offline feature. I integrated that in my app just by writing one line of code in my main.dart file after initializing Firebase await FirebaseDatabase.instance.setPersistenceEnabled(true);
Question 1 :
I couldn't able to understand the database.keepSynced(true) function because without using this line of code, my app is persisting old as well as fetching new updated data, so what this exactly does ?
Question 2 :
How could I prevent the write operations when a user is offline, because I read that after setting persistence enabled, it makes a queues of write operations and update them when user gets online, so how could I stop this ?
Question 3 :
Is this persistence feature going to work in IOS device as well or need some permission settings first ?
Thanks
When you call FirebaseDatabase.instance.setPersistenceEnabled(true) you're allowing Firebase to create a local file on the device where it persists any data it's recently read, and all writes that are pending while the device is offline.
When you call keepSynced(true) on a node, you are telling the SDK to always keep that node synchronized. It essentially creates a onValue listener on the node without any handler code, so you're purely doing this to keep the data synchronized for when the device does go offline.
By combining keepSynced(true) with setPersistenceEnabled(true), you're specifying that you want the app to continue working when it's offline across restarts, and which data is needed for that.
If you call keepSynced(true) on the root of your database, you're telling the SDK to synchronize all data in the database to the disk cache. While this may initially be a quick way to get offline mode for your app working, it typically won't scale when you more people start using your app.
If you only want to allow write operations while the client has a connection to the database backend, you can register a local listener to the .info/connected node, which is a true value when there is a connection and false otherwise.
Note that Firebase doesn't require this, as it queues the pending writes and executes them when the connection is restored. In general, I'd recommend working with the system here instead of against it, and also trying to make your app work gracefully in the offline scenario. In many cases there is no need to disable functionality while the app is offline.
Offline disk persistence is available on Android and iOS, but not on web.
I am using OneSignal in my Flutter application to receive notifications. I have created my own table within Flutter to save notifications but I cannot save messages that have been sent whilst the app has been terminated. However, I have found that the OneSignal package has it's own database installed on the device which captures everything no matter what state the application is in! So, to avoid duplicating data I want to use this database instead. It is located outside of the flutter app folder which is my issue (see image below).
How do I access this database? Does anybody know if it is the same on iOS (I haven't been able to check at this time).
Many thanks.
It's important to understand that if an app has been terminated, any onReceived handler will not be fired. We don't recommend using the local db at all. Your best bet is to use the respective notification extender services for each platform. Though you will have to write native code for this! Cheers
Edit:
ANDROID - NotificationExtenderService - This can be setup to receive silent notifications when your app is not running or to override how notifications are shown in the notification shade. See the Background Data and Notification Overriding section to set this up.
I noticed that if I send some data to the database while internet connection is dropped, after I reconnect the data doesn't get resent. Is this feature not available for unity or am I missing something? This docs say that this should work but it doesn't : https://firebase.google.com/docs/database/unity/save-data#write_data_offline
Ok, I found out what was the issue. On Android, you need to enable external write permission in order for that feature to work, which is silly, because we do not want to enable this permission. Firebase should save its data to the perstistent data path which doesn't require any permissions.