I am creating an app and I have some local data in the app which I have to sync to the server. I can only sync the data to the server if there is internet connection. If there is no internet connection, then the app will log the data temporarily and once the internet connection is back, the data will sync the data to the server.
My question here is how often should I check whether there is internet connection. Should I do this in the background? Or is it better to have the internet connections checks at certain screens of the app and if there is data, then I sync the data?
Need some suggestions on how to go about solving this problem.(Note: Intend to use Reachability to check for Internet connection)
Related
I was testing my iOS app and continuously fetching data and decided to hop off my Wifi with the app still open and test it on LTE. In my debug logs I see this error "[FirebaseFirestore][I-FST000001] WatchStream (10d119db8) Stream error: 'Unavailable: Network connectivity changed'" being printed. The fetching of the data no longer works and not because my LTE is too slow. I prove this because it seems to regain connection and fetching of the data works fine if I kill the app and reopen it (tested this many times). I can't seem to find anywhere online on how to reconnect/refresh a connection to Firestore on a network connectivity change like this one. Anybody got any ideas?
The error message that you have mentioned clearly states an issue with the connectivity changes that the Firebase app is not able to keep up while loading data and switching networks during this.Though Firebase apps automatically handle temporary network interruptions. Cached data is accessible while offline and Firebase resends any writes when network connectivity is restored.Add a listener to the Firestore client to detect when the connection status changes. You can then use the Firestore client's reconnect() method to attempt to re-establish the connection to the Firestore server. Additionally, you can also use the onReconnect() method to register a callback that will be called when the Firestore client successfully reconnects to the Firestore server.
Also check the following documentation for your reference:
Able to Write firebase until restarting wifi connection
Firebase firestore use two different instance
Creating network listener accessible to ios application
How to detect absent network connection
How does transactions in firestore error out
After more research, I learned Firestore attempts to reestablish a connection on it's own. I was testing other features in my app to see if this was true and it was! The other features were working.
So i decided to look elsewhere in my code and discovered I was not properly handling a condition where my data was not being cleared before a reload of the table. This was happening I suppose due to the auto Firestore data caching when losing a connection? In order to reload the table the data needed to be cleared first. So it was a bug in my code completely unrelated to the network connection.
I'd like to find out if a Wi-Fi has internet connection or not. Sometimes our devices are connected with Wi-Fi, but the Wi-Fi doesn't have an internet connection.
NWPathMonitor or Connectivity thirdparty. I tried it, but it is not fool proof.
I know I can write a method to ping a server, but this is a very ineffective and costly affair.
Any ideas?
The only way to know for sure if a network has an Internet connection is to try and make a connection to the Internet. There is nothing on the device that you can query to get the answer.
Even if there were it would have to resort to polling/pinging since connectivity can change at any instant.
A WiFi connection can transition from no-Internet to Internet when the user completes a Hotspot login form. A WiFi connection can transition from Internet to no-Internet if the upstream Internet connection fails in some way. A WiFi connection may be connected to the Internet but not be able to communicate with the specific host your app needs due to a routing failure or some other issue.
For all of these reasons Apple actively discourages "pre flight" checks. Even if a check passes a network operation can still fail due to a change in network status that occurred a microsecond later. It is also unnecessary overhead as it is generally a safe assumption that most devices have some sort of Internet connection most of the time, particularly if the device is an iPhone.
You need to handle errors anyway. Just try the operation and see what happens. If there is no network connection it will fail pretty quickly.
If you want to provide more feedback to the user then you could begin actively checking Internet connectivity with exponential back off in response to a connection error.
That way your app isn't constantly "pinging".
You can also use the error to start a process that uses SCNetworkReachability to check connectivity for you. Rather than using that framework directly, particularly if you are writing in Swift, you might like to use something that wraps it and makes it more accessible like Ashley Mills' Reachability.swift
Apple added in iOS 7 new possibility to execute activities in the background, fetch mode, which can be used e.g. for Twitter, when frequently small amounts of data need to be downloaded (official description: The app regularly downloads and processes small amounts of content from the network.),Apple Document Reference
However, I need to use a TCP connection.
Does anyone know whenever TCP connection may be used in such scenario between the calls?
The scenario is:
I open the TCP socket in the beginning and then the application can use it in these fetch calls. Of course handlers are needed when the TCP connection drops, but there is a difference between rare necessity to reconnect and constant (i.e. in each fetch iteration) need to reconnect.
Hi,
In my application I have a small registration form. I want the functionality to work like following
If the device is connected to network, data should be stored directly
in server.
If device is not connected to network, then it should
store temporarily in device and when the device connects to network
it has to store the data in online server.
Please tell if this is possible and what is the right method to do it.
Thanks.
Yes, it's possible
You can store whatever state you want locally and use one of the Reachability classes to see if a network connection is available and then attempt to run your network code to upload this state to the server.
I'm creating an app that allows the user to work without internet connection. When he wants to publish his progress to the web server it gets stored in the device's database. If he isn't connected to the internet I have to send it when he gets internet connection back.
How can I run the code to send the data to the database when internet connection is available again?
In Android I used a broadcast receiver to listen for a "connection changed" event to accomplish this but I don't seem to find a way in iOS to do the same.
You could try to check if the device has an active internet connection (3G and/or Wi-Fi for iPhone, Wi-Fi for iPad/iPod Touch), there are many ways to do that but the simplest in my opinion is to use Reachability.
Check this similar question for more informations about Reachability and how to implement it in your project. It can even be used in a if statement so you can make the user able to publish or not depending on his internet connection.