Actually I found the solution after some tweaking. It was not obvious at first.
My topic should have been “How to Recover From Network Error in Ionic?” but at first saw the problem in Pull to Refresh.
Anyway here is the code I first used in my controller. Controller have $http.get (outer get) and inside it is pull to refresh code which basically does same thing when user manually pull to refresh it.
$http.get('fetch data from API', {timeout: 15000})
.then(function(response) { //success
//do something
//broadcast that refresh is complete
$scope.$broadcast('scroll.refreshComplete');
//pull to refresh
$scope.doRefresh = function() {
//get data and process
$http.get('fetch data from API', {timeout: 15000})
.then(function(response) {
//do something
//broadcast that refresh is complete
$scope.$broadcast('scroll.refreshComplete');
},
function(error){
$ionicPopup.alert({
title: "Connection Error",
content: "This App requires network connection. Check if network is turned on and connection is okay. Try pulling to refresh again."
});
$scope.$broadcast('scroll.refreshComplete');
});
}
},
function(error) { //error
$ionicPopup.alert({
title: "Connection Error",
content: "This App requires network connection. Check if network is turned on and connection is okay. Try pulling to refresh again."
});
$scope.$broadcast('scroll.refreshComplete');
}
});
The reason I was seeing the behavior of Pull to refresh not executing is that I have implemented on the “.then” (or success) part. Actually it was working fine. What happened is when app is loaded with network connection OFF, it executed the outer error (controller’s error) function, which doesn’t have pull to refresh code. So expecting Pull to Refresh come to rescue after error was not working.
In my second code I duplicated the Pull to refresh code on controller’s error function also and it worked perfectly as expected. Though duplicate codes, seems it have to be done this way.
$http.get('fetch data from API', {timeout: 15000})
.then(function(response) { //success
//do something
//broadcast that refresh is complete
$scope.$broadcast('scroll.refreshComplete');
//pull to refresh
$scope.doRefresh = function() {
//get data and process
$http.get('fetch data from API', {timeout: 15000})
.then(function(response) {
//do something
//broadcast that refresh is complete
$scope.$broadcast('scroll.refreshComplete');
},
function(error){
$ionicPopup.alert({
title: "Connection Error",
content: "This App requires network connection. Check if network is turned on and connection is okay. Try pulling to refresh again."
});
$scope.$broadcast('scroll.refreshComplete');
});
}
},
function(error) { //error
$ionicPopup.alert({
title: "Connection Error",
content: "This App requires network connection. Check if network is turned on and connection is okay. Try pulling to refresh again."
});
$scope.$broadcast('scroll.refreshComplete');
//pull to refresh
$scope.doRefresh = function() {
//get data and process
$http.get('fetch data from API', {timeout: 15000})
.then(function(response) {
//do something
//broadcast that refresh is complete
$scope.$broadcast('scroll.refreshComplete');
},
function(error){
$ionicPopup.alert({
title: "Connection Error",
content: "This App requires network connection. Check if network is turned on and connection is okay. Try pulling to refresh again."
});
$scope.$broadcast('scroll.refreshComplete');
});
}
}
});
Since both $http.get (controller’s get and pull to refresh get) function have timeout set. It is even responsive when network error occurs on the middle of something.
Case 1
- Network turned OFF
- Opening app shows the error message.
- Turn on (or fix the network) then Pull to refresh to continue.
Case 2.
- Initially Network is fine and app is loading and working fine
- Network error occurs in the middle of operation. Either controller’s function of doRefresh function notices (if user was doing pull to refresh at that time) and show error message.
- After user fixes the network, all user have to do is pull to refresh the data.
By the way there is another approach to that also.
- When network error is detected, user get error message and button to refresh the page.
- When clicked, it will reload the entire page using
$window.location.reload(true);
so if network is okay, it should load and fetch data normally.
For me personally, former solution is sleek.
I went on to explain a bit hoping it will help newbie like me wanted to implement similar feature.
Thanks anyway for the help.