Search code examples
iosobjective-crestkit

RestKit 0.20.0-rc1 mappingResult is empty after 304 Not Modified


I have a simple client application that points to a Rails-backed API. It gets non-managed objects as follows:

[[RKObjectManager sharedManager] getObjectsAtPath:@"places" params:nil success:...]

The problem I face is that RestKit does not perform any mapping after a refresh because the response is 304 Not Modified.

However, there is a JSON payload when examining the operation.HTTPRequestOperation.responseData. How do I get restkit to map even when the response is 304 Not Modified.


Solution

  • Just run into same problem in my project.

    Looks like in RestKit 0.20 cache was totally reworked (actually it was removed, watch github issue #209). Now when receives NOT MODIFIED response, it doesn't parse cached response body. Instead it tries to load objects from persistent store using so called "RKFetchRequestBlock's". You can read more about it here: http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html

    So you will need to add RKFetchRequestBlock for each URL that can return 304 response. Another option is to disable NSURLCaching, which is not trivial in RestKit 0.20. I use following code:

    ELObjectManager.h

    @interface RKObjectManager ()
    // So we can call [super requestWithMethod:...]
    - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                          path:(NSString *)path
                                    parameters:(NSDictionary *)parameters;
    
    @end
    
    @interface ELObjectManager : RKObjectManager
    @end
    

    ELObjectManager.m

    #import "ELObjectManager.h"
    
    @implementation ELObjectManager
    
    - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                          path:(NSString *)path
                                    parameters:(NSDictionary *)parameters
    {
        NSMutableURLRequest* request = [super requestWithMethod:method path:path parameters:parameters];
        request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
        return request;
    }
    
    @end
    

    And then use this class instead of RKObjectManager

    [RKObjectManager setSharedManager:[ELObjectManager managerWithBaseURL:...]