Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Apply Caching and Offline Storage in your iOS App

DZone's Guide to

Apply Caching and Offline Storage in your iOS App

Learn how to easily enable offline caching and storage for your iOS mobile app so your users can access and use it even when the network is unavailable.

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

The App42 iOS SDK now supports iOS offline caching and storage that will make your iOS application responsive even if the network is not available so the user can use your application flawlessly without any trouble. iOS offline caching enables your application to read data from the local cache if the network is not available. Using this feature is quite straightforward and requires a single line of code to enable.

iOS caching data and iOS offline storage can be used in isolation or they can be used along with each other based on your app requirement. You can enable these features as explained below.

Offline Caching

Caching allows an application to read data from its local cache depending on the policy that you have set. App42 allows three different cache policies, which we discuss below.

  1. APP42_CACHE_FIRST: This policy enables apps to first look for the cached response for all the READ requests and returns the valid cached data if available, otherwise the request will be sent to the server. A valid cached response means that the cached response is not expired.
  2. APP42_NETWORK_FIRST: This policy will try to send the request to the server if the network is available, otherwise it will look for the cached response.
  3. APP42_NO_CACHE: This is a default policy and follows the normal flow, i.e. it sends every request directly to the server.

Note: The expiry time for cached response is set by default to 24 hours, but you can always overwrite using our API, as explained below:

[code java][[App42CacheManager sharedCacheManager] setPolicy:APP42_CACHE_FIRST]; 
//Setting the Policy
[[App42CacheManager sharedCacheManager] setExpiryInMinutes:<EXPIRY_TIME_IN_MINUTES>]; 
// Setting expiry time[/code]

Offline Storage

Before talking about the details, let us go through some use cases.

  1. The user is playing your game and data, such as the score, needs to be saved to the cloud, but there is no network.
  2. The user wants to post his status in a social app and there is no network.
  3. The user wants to modify his existing note, but there is no network available.

There may be such use cases, and as a result, the application may lose the activity of that particular user, which may be a disappointment. This is where this feature will help. Offline storage allows the application to save requests (POST, PUT, and DELETE) locally in the application cache when it runs offline and sends them to the server when it goes online. Enabling this feature is a single-step process:

[code java][App42API setOfflineStorage:YES]; 
//Enabling Offline Storage[/code]

With the above two lines of code, you have enabled two great features in your iOS app.

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:
caching ,ios ,mobile ,mobile app development

Published at DZone with permission of Rajeev Ranjan. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}