Caching

Caching will enable your iOS application to be responsive even if the network is not available and user can use your application flawlessly without any trouble. Using this feature is quite straight forward and requires a single line of code to enable it.

Offline caching enables your application to read data from local cache if network is not available. There are different cache levels that can be set using API and have been explained below:

Cache Policies
  • APP42_CACHE_FIRST

Setting this policy will enable all read data to be first looked in to cache if data is available and not expired then it will return from cache otherwise network request will be made and cache will be updated for the same. You can set cache expiry time by using API as explained below. By default cache expiry is set to an hour.

  • APP42_NETWORK_FIRST

This policy enables data to be fetched from network and then updates cache. If network is not available, data is fetched from cache.

  • APP42_NO_CACHE

By default App42 SDK uses this policy and does not use any cache and always reads data from network only.

Setting Cache Policy

To set the cache policy, you have to initialize the below code in your application:

[[App42CacheManager sharedCacheManager] setPolicy:APP42_CACHE_FIRST]; 

You can also set cache expiry means when to update cache, If you have set network policy then cache will be updated every time record is fetched however if cache policy is set as CACHE_FIRST then cache will be updated after every one hour, you can also set custom cache expiry in minute as shown below.

[[App42CacheManager sharedCacheManager] setExpiryInMinutes:<EXPIRY_TIME_IN_MINUTES>]; 
Sample Code
		
UserService *userService = [App42API buildUserService];
NSString *userName = @"Nick";    
[userService getUser:userName completionBlock:^(BOOL success, id responseObj, App42Exception *exception) {
        if (success)
        {
            User *user = (User*)responseObj;
            if (user.isFromCache)
            {
                // Response from cache
                NSLog(@"UserName=%@",user.userName);
                NSLog(@"Email=%@",user.email);
            }
            else
            {
                // Response from server
                NSLog(@"UserName=%@",user.userName);
                NSLog(@"Email=%@",user.email);
            }
        }
        else
        {
            NSLog(@"Exception = %@",[exception reason]);
            NSLog(@"HTTP error Code = %d",[exception httpErrorCode]);
            NSLog(@"App Error Code = %d",[exception appErrorCode]);
            NSLog(@"User Info = %@",[exception userInfo]);
        }
    }];

If Response is from cache you will get isFromCache flag to true in the response so you can identify that data is real time or data is coming from cache.

Offline storage

Offline storage allows you to post data locally in case network is not available and syncs it with server later when network is available. This is useful in many scenarios e.g. if your user is playing a game and has scored something on completion of a specific level. However, at the time of posting of score if network is not available his score might get lost and his efforts will go waste. This can be avoided using offline caching where his score will be saved locally in case of network unavailability and will be later synced with server when network availability resumes.

  • Offline Storage can be set in your app as shown below.
[App42API setOfflineStorage:YES];
  • You have to handle response whether reponse coming locally or from server as shown below.
//Set Offline Storage to True
NSString *gameName = @"<Enter_your_game/level_name>";
NSString *userName = @"Nick";
 double gameScore = 450;

[scoreboardService saveUserScore:gameName gameUserName:userName gameScore:gameScore completionBlock:^(BOOL success, id responseObj, App42Exception *exception)
     {
        if (success)
        {
            Game *game = (Game*)responseObj;
            if (game.isOfflineSync)
            {
                //Request is saved in cache
	NSLog(@"Information is Stored in cache, will send to App42 when network is available");
                NSLog(@"Offline Response = %@",game.strResponse);
            }
            else
            {
                //Response Received From Server and is Succeseful
                NSLog(@"Game Name=%@",game.name);
                NSLog(@"Description =%@",game.description);
             }
        }
        else
        {
            NSLog(@"Exception = %@",[exception reason]);
            NSLog(@"HTTP Error Code = %d",[exception httpErrorCode]);
            NSLog(@"App Error Code = %d",[exception appErrorCode]);
            NSLog(@"User Info = %@",[exception userInfo]);
        }
    }];

The expected behavior of above code is that if network is available it give response from server and if network is not available response comes locally.