Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:38:49+00:00 2026-06-08T02:38:49+00:00

I am wanting to use RestKit for an application that needs to work both

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:38:51+00:00Added an answer on June 8, 2026 at 2:38 am

    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];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wanting to use an extension for Python that I found here , but
I am wanting to use core plot but before I work on implementing it,
I am wanting to use the g:timeZoneSelect tag within my application, problem is im
I'm wanting to use some newer software that requires Python 2.6 , and we
I'm developing a new ASP.NET MVC 2.0 application and wanting to use the new
I'm trying to write a java application in Eclipse. I'm really wanting to use
Wanting to use: Ajax Event Calendar but the dates are in american format, how
I have been wanting to use a git command that saves a stash without
I am wanting to use the Symfony\Component\Process\ProcessBuilder class and can see that it is
I'm wanting to use android:configChanges=orientation|keyboardHidden for a few of my activities so that my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.