@saryu (Saryu) posted a new topic, subject "I need to fetch data from server & put into local DB in background". It read:

   I need to fetch data from server & put into local DB in background. I mean this syncing should happen automatically in night. I am using SQLite as local app DB.

So @eivanov (Eivanov) replied

    You can use $interval angular service for that.
    
      * register getDataFromBackend function on app.js (onReady) or app's main controller with $interval service
    
      * getDataFromBackend should getting data from backend and return promise, then resolving promise with format recived data and save data to the sqlite database.
    [code]       function updatePrograms() {
                  return Database.getCategoryLastUpdateDate().then(function (date) {
                      Backend.getProgramList(date).then(function (res) {
                          return res == null ? $q.when(null) :
                          Database.savePrograms(res.data).then(function () {
                              $log.debug('Updater: update Programs done');
                              return res;
                          });
                      });
                  });
              };
        
    [/code]
    
    $interval(function() {  
    updatePrograms();  
    }, 5 _60_ 1000);
    
    
    
    
    But i am guessing your app should be running for this, if not you can update data on app start.
    
    If this is not enough and you want to update data when app is closed, you can write your own plugin with broadcastReciever (for android) implementation like this.

After which, @saryu (Saryu) added

    Hey thanx for the insights. But with background I meant when I am not on my App.
    
    The code sent by you is running fine when I am accessing my app's other screens but that is not my requirement.

After which, @eivanov (Eivanov) added

    Try to use cordova-plugin-background-mode or cordova-plugin-background-download. Also you can write custom plugin.


{'source': 'discourse', 'url': 'https://forum.ionicframework.com/t/i-need-to-fetch-data-from-server-put-into-local-db-in-background/62362', 'title': 'I need to fetch data from server & put into local DB in background', 'views': 1971, 'has_accepted_answer': False, 'word_count': 280, 'like_count': 0, 'reply_count': 2, 'participant_count': 2, 'posts': 4, 'tags': '', 'author': '@saryu (Saryu)', 'participants': '@saryu (Saryu), @eivanov (Eivanov)', 'image_url': [None], 'created_at': '2016-09-02T06:18:02.736Z', 'last_posted_at': '2016-09-05T03:51:23.286Z'}