Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Online and offline support for RestKit iOS applications

I am wanting to use RestKit for an application that needs to work both while on and offline.

I may be misunderstanding how RestKit works, but I thought that this was (fairly) easy to implement.

In a scratch test iOS app I set things up as follows:

// setup the client
NSString* URL = @"http://10.211.55.5:3000/api";
RKClient* client = [RKClient clientWithBaseURL:URL];
client.username = @"[email protected]";
client.password = @"password";

// setup caching
client.cachePolicy = RKRequestCachePolicyLoadIfOffline | RKRequestCachePolicyLoadOnError | RKRequestCachePolicyTimeout;
client.requestCache.storagePolicy = RKRequestCacheStoragePolicyPermanently;

// setup managed object store
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:URL];
RKManagedObjectStore* objectStore = [RKManagedObjectStore   objectStoreWithStoreFilename:@"RestKitCoreDataTest.sqlite"];

// connect my cache implementation
MyCache* cache = [[MyCache alloc] init];
objectStore.managedObjectCache = cache;
objectManager.objectStore = objectStore;

// setup mapping    
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[userMapping mapKeyPath:@"email" toAttribute:@"email"];
[userMapping mapKeyPath:@"firstname" toAttribute:@"firstname"];
[userMapping mapKeyPath:@"surname" toAttribute:@"surname"];
userMapping.primaryKeyAttribute = @"identifier";
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@""];

// setup routes
RKObjectRouter* router = [objectManager router];
[router routeClass:[User class] toResourcePath:@"/users/:identifier"];

The User object is implemented as required for CoreData support:

@interface User : NSManagedObject

@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * surname;

@end

@implementation User

@dynamic identifier;
@dynamic email;
@dynamic firstname;
@dynamic surname;

@end

Here is MyCache. Note that I am not bothering to check resourcePath since this is just for trying things out, and I have one path anyway.

@implementation MyCache
- (NSArray*)fetchRequestsForResourcePath:(NSString*)resourcePath
{
    NSFetchRequest* fetchRequest = [User fetchRequest];
    return [NSArray arrayWithObject:fetchRequest];
}

-(BOOL)shouldDeleteOrphanedObject:(NSManagedObject *)managedObject
{
    return true;
}
@end

I then make a call to the server to get the user with id 123, at the path "/api/users/123":

User* user = [User object];
user.identifier = [NSNumber numberWithInt:123];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager getObject:user delegate:self];

This works fine. But, when I then disconnect the wifi on my Mac, the above code does not retrieve the user from the sqlite database.

I get the following error instead in the delegate's objectLoader:didFailWithError:

2012-03-01 11:44:09.402 RestKitCoreDataTest[1989:fb03] error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x6b89aa0 {NSErrorFailingURLStringKey=http://10.211.55.5:3000/api/users/123, NSErrorFailingURLKey=http://10.211.55.5:3000/api/users/123, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x6b81ac0 "The request timed out."}

I thought that by virtue of specifying that the cache should be used when there's a timeout, with: "RKRequestCachePolicyTimeout", I would have expected the user to be retrieved from the local cache.

The cache does contain a user record with ID 123 -- in the ZUSER table, with 123 in the ZIDENTIFIER column.

Is there a step that I am missing to get this to work? Maybe another delegate method that needs to be handled, or is called, when the cache is hit after the timeout? Or am I trying to do something which is not necessarily something you'd get "out of the box" with RestKit?

Cheers.

like image 268
Diego Barros Avatar asked Mar 01 '12 01:03

Diego Barros


1 Answers

You might use the Reachability Class to determine wether your client is offline or not. I use this great class very often in every project that requires an internet connection.

You simply start the notifier to a specific host. In all of your viewController you now just have to register methods to the NSNotificationCenter to set a BOOL isOnline for example.

Doing this practice you can do beautiful stuff in your app like overlaying the app with a smooth "Offline" message.

https://gist.github.com/1182373

EDIT

Heres one example from the login screen from one of my projects (sorry for this quantity of code but this is my complete implementation):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //// Some stuff ////
    [[Reachability reachabilityWithHostname:@"apple.com"] startNotifier];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
}
- (void)reachabilityChanged:(NSNotification *)note
{
    if ([[note object] isReachable]) {
        CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        [scale setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:0.0], nil]];
        [scale setDuration:0.3];
        [scale setRemovedOnCompletion:NO];

        [[offlineView layer] setTransform:CATransform3DMakeScale(0, 0, 1.0)];
        [[offlineView layer] addAnimation:scale forKey:@"scale"];
        [[offlineView layer] setTransform:CATransform3DIdentity];

        [UIView animateWithDuration:0.3 animations:^{
            [offlineView setAlpha:0];
        } completion:^(BOOL finished){
            if (finished) {
                [offlineView removeFromSuperview];
            }
        }];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    }
    else {
        CGRect screenFrame = CGRectMake(0, 0, 320, 480);

        offlineView = [[UIView alloc] initWithFrame:screenFrame];
        [offlineView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
        [offlineView setAlpha:0];

        offlineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        [offlineLabel setFont:[UIFont fontWithName:@"Verdana" size:30.0]];
        [offlineLabel setBackgroundColor:[UIColor clearColor]];
        [offlineLabel setTextAlignment:UITextAlignmentCenter];
        [offlineLabel setTextColor:[UIColor whiteColor]];
        [offlineLabel setCenter:[offlineView center]];
        [offlineLabel setText:@"OFFLINE"];

        [offlineView addSubview:offlineLabel];
        [[self window] addSubview:offlineView];

        [UIView animateWithDuration:0.3 animations:^{
            [offlineView setAlpha:1.0];
        }];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
    }
}
like image 173
Julian F. Weinert Avatar answered Nov 07 '22 20:11

Julian F. Weinert