Search code examples
mobile-applicationtrigger.ioclientside-caching

Caching best practice for mobile hybrid/bridge app development


I really need to limit any unnecessary network traffic and server trips. Solution: common sense caching. (I am not going to cache everything under the sun).

However, after reading through the Caching Files documentation and implementing a couple of quick examples, when is the best time to cache an ajax json result? Sure I can do the usual cache/no cache check each time my view is displayed. But is there a way to perform an asynchronous load during initial application startup to prefetch remote data that I know the user is going to need? Is using the connectionStateChanged event the only way (or closest way)? Is there a way to "hook" into the splash screen (yes, I know Apple wants the splash screen for mostly transition)? window.onload?


Solution

  • So if I understand you correctly, you're looking for a way to asynchronously fetch remote resources once for each time the app starts up, and cache those data away?

    Our request module is asynchronous by nature, so you could simply drop in a forge.request.ajax to start fetching an Ajax response, then store it away in the preferences module.

    Although it's probably identical in practice, you could even wrap it in a setTimeout to make it even more asynchronous:

    setTimeout(function ()
      forge.request.ajax({
        url: 'http://example.com/method.json',
        success: function (data) {
          forge.prefs.set("method.json-cache", data);
        }
      });
    }, 10);