chourobin
chourobin

Reputation: 4124

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.

Upvotes: 2

Views: 685

Answers (2)

yichen
yichen

Reputation: 511

I am having the same problem, what's worse is that the server status code is actually 200 instead of 304. The mappingResult is null as well, and the actual data can be found in operation.HTTPRequestOperation.responseData as the original post says. I am running 0.20.0. Please let me know if there is a workaround for me.

The following shows the mappingResult empty even though the status code is 200. The responseData is too long to post. I am also using Core Data integration of RestKit.

2013-04-25 16:38:14.244 MyApp[58584:c07] I restkit.network:RKHTTPRequestOperation.m:185 GET 'http://localhost:3000/api/search?access_token=3ad3b8c4a5fed9503a80c1f6afebab47&keywords=art' (200 OK) [0.0047 s]
(lldb) po mappingResult
$0 = 0x079a1880 <RKMappingResult: 0xac64c80, results={
    "<null>" =     (
    );
}>
(lldb) po operation
$1 = 0x07995fd0 <RKManagedObjectRequestOperation: 0x7995fd0, state: Successful, isCancelled=NO, request: <NSMutableURLRequest http://localhost:3000/api/search?access_token=3ad3b8c4a5fed9503a80c1f6afebab47&keywords=art>, response: <NSHTTPURLResponse: 0x7e51fc0 statusCode=200 MIMEType=application/json length=58781>>
(lldb) 

Upvotes: 1

glyuck
glyuck

Reputation: 3397

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:...]

Upvotes: 2

Related Questions