Synch App data with server in phonegap

Posted By : Avilash Choudhary | 23-Dec-2014

Sync App Data with server in Phonegap

In every mobile application, there are lots of API's that need to be hit to get data from server. Every user want that, when he/she click on any button the result should be fast but network calls may take more time to fetch data from server. So the best option is to synch server data with mobile application and use some local storage of html or local database i.e sqlite to store that data.

 

To synch data with server, we need to make a function which will fire after a time interval to fetch data from server. that function will contain all api calls needed in app to get different information from server.

 

This synch call will work on background that will not affect user activity. In synch, we can use timestamp.

Like in our application, we need to get different posts and questions for the user, in this case, we hit the Api with default timestamp so server returns posts and questions and the timestamp that we can save in our local storage. next time  the Api will be hit with timestamp we have got from server in previous call. So the server will return posts and questions that was created or updated after that timestamp. This all procedure will work in background.


The below function will be fired starting of application which will the below reloadAppData function after some period.

 

 home.startAppReloadInterval = function() {
	home.interval = setInterval(function() {
		reloadAppData(function() {
		});
	}, 120000);
};

reloadAppData = function(callback) {
		var completeCount = 0;
		checkCompleteCount = function() {
			if (completeCount == 3) {
				callback();
				console.log("complete");
			}
		};
			home.storeExpertStats(function() {
				console.log("success expert stats data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			}, function() {
				console.log("Failure expert stats data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			});
			profile.expertProfile(function() {
				console.log("success expert profile data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			}, function() {
				console.log("Failure expert profile data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			});	
			home.getSyncData(function() {
				console.log("success  post question data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			}, function() {
				console.log("Failure post question data");
				completeCount = completeCount + 1;
				checkCompleteCount();
			});		
};

About Author

Author Image
Avilash Choudhary

Avilash has excellent experience in developing mobile and web applications using jQuery , Javascript and PhoneGap. His hobbies are watching and playing cricket.

Request for Proposal

Name is required

Comment is required

Sending message..