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 = @"me@email.com";
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.
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):