2013-02-13 35 views
2

我有一個簡單的客戶端應用程序指向Rails支持的API。它得到非託管對象如下:RestKit 0.20.0-rc1 mappingResult在304之後爲空未修改

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

我所面臨的問題是,RestKit不刷新後執行任何映射,因爲響應304不會被修改。

但是,檢查操作時有一個JSON負載.HTTPRequestOperation.responseData。即使響應304未修改,我如何獲得restkit映射。

回答

2

就在我的項目中遇到同樣的問題。

看起來像在RestKit 0.20緩存完全重寫(實際上它被刪除,觀看github問題#209)。現在,當收到NOT MODIFIED響應時,它緩存了響應主體doesn't parse。相反,它會嘗試使用所謂的「RKFetchRequestBlock's」從持久存儲中加載對象。您可以在這裏閱讀更多關於它的信息:http://restkit.org/api/latest/Classes/RKManagedObjectRequestOperation.html

因此,您需要爲每個可以返回304響應的URL添加RKFetchRequestBlock。 另一種選擇是禁用NSURLCaching,這在RestKit 0.20中並不重要。我用下面的代碼:

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 

然後用它代替RKObjectManager

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

嗨加尼克,謝謝你的回答。實際上我發現我沒有添加pathPattern:對於每個響應描述符,如果使用CoreData,RestKit默認爲RKManagedObjectRequestOperation。一旦我爲每個響應對象指定了路徑模式,Restkit就可以區分並使用正確的RKObjectRequestOperation而不是RKManagedRequestOperation。我認爲這現在解決了我的問題。 – chourobin 2013-02-28 16:30:34

+0

我嘗試了@glyuck提出的解決方案(子類化RKObjectManager並將cachePolicy設置爲NSURLRequestReloadIgnoringLocalAndRemoteCacheData)無濟於事。正如所見[本文](http://nshipster.com/nsurlcache/),「NSURLRequestReloadIgnoringLocalAndRemoteCacheData」尚未在「NSURLRequest」中實現。 **忍者編輯:**我的下一步是修改我的API服務,所以它永遠不會發送'304'標頭。 – rilla 2013-04-24 02:41:01

1

我有同樣的問題,這個類,更糟糕的是服務器狀態碼實際上是200而不是304. mappingResult是空的以及實際的數據可以在操作中找到.HTTPRequestOperation.responseData作爲原始文章說。我正在運行0.20.0。請讓我知道是否有解決方法。

即使狀態碼爲200,以下顯示mappingResult爲空。響應數據太長而無法發佈。我也在使用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) 
+0

我有一個解決此問題的方法,在查詢字符串中添加緩存查看器參數。 – yichen 2013-04-26 04:02:27

相關問題