2012-08-01 18 views
3

我在看RestKit框架及其工作原理。 然後,我創建一個簡單的項目,將調用Github api並將數據加載到tableview中 - 在我閱讀過一些教程後 - 關於從遠程加載和映射它工作正常並加載到tableview,但是當我試圖獲取從數據存儲-offline模式 - 應用程序會崩潰,這是日誌:RestKit - fetchFromDataStore(緩存)問題

2012-07-29 16:21:41.611 RKGithubClient_FromZtoH[29181:c07] I restkit:RKLog.m:33 RestKit initialized... 
2012-07-29 16:21:53.161 RKGithubClient_FromZtoH[29181:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.' 
*** First throw call stack: 
(0x20b9022 0x197acd6 0xf15871 0x643c 0x42e5 0x3e14 0x4697 0x53838f 0x5385eb 0x5394ed 0x4a6a0c 0x4abf92 0x4a614b 0x495550 0x495670 0x495836 0x49c72a 0x2a6a 0x46d386 0x46e274 0x47d183 0x47dc38 0x471634 0x25e1ef5 0x208d195 0x1ff1ff2 0x1ff08da 0x1fefd84 0x1fefc9b 0x46dc65 0x46f626 0x259d 0x2505) 
terminate called throwing an exception 

這是fetchFromDataStore方法:

- (void)fetchFromDataStore 
{ 
    NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath]; //self resourcePath]]; 
    self.repos = [GithubRepo objectsWithFetchRequest:request]; 
    [self.tableView reloadData]; 
} 

任何想法,爲什麼發生的事情,因爲我已經創建實體它們,你可以檢查此鏈接的源代碼:

Source Code

此外,是否有任何有用的示例或教程RestKit緩存?

感謝,

回答

2

最後,我回答吧..

問題連接到的事實是,當我問: NSFetchRequest *請求= [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath]; myViewController.m上的 ,返回空。 發生這種情況的原因是因爲對象映射器尚未配置爲正確處理核心數據緩存。 我使用:

- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern; 

,而我應該使用:

- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern withFetchRequestBlock:(RKObjectMappingProviderFetchRequestBlock)fetchRequestBlock; 

其住在對象映射的核心數據擴展。您提供的額外塊用於根據資源路徑中傳遞的參數(本例中爲用戶RestKit)來決定需要哪個獲取請求。在具體的情況下,我想要返回一個提取請求,當回購的用戶==「RestKit」時尋找回購站。

的文檔,雖然很難找到這個方法,是非常有幫助的:

/** 
Configures an object mapping to be used when during a load event where the resourcePath of 
the RKObjectLoader instance matches resourcePathPattern. 

The resourcePathPattern is a SOCKit pattern matching property names preceded by colons within 
a path. For example, if a collection of reviews for a product were loaded from a remote system 
at the resourcePath @"/products/1234/reviews", object mapping could be configured to handle 
this request with a resourcePathPattern of @"/products/:productID/reviews". 

**NOTE** that care must be taken when configuring patterns within the provider. The patterns 
will be evaluated in the order they are added to the provider, so more specific patterns must 
precede more general patterns where either would generate a match. 

@param objectMapping The object mapping to use when the resourcePath matches the specified 
resourcePathPattern. 
@param resourcePathPattern A pattern to be evaluated using an RKPathMatcher against a resourcePath 
to determine if objectMapping is the appropriate mapping. 
@param fetchRequestBlock A block that accepts an individual resourcePath and returns an NSFetchRequest 
that should be used to fetch the local objects associated with resourcePath from CoreData, for use in 
properly processing local deletes 
@see RKPathMatcher 
@see RKURL 
@see RKObjectLoader 
*/ 
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern withFetchRequestBlock:(RKObjectMappingProviderFetchRequestBlock)fetchRequestBlock; 
+0

什麼你fetchRequestBlock最終看起來怎麼樣? – Erik 2012-09-26 04:33:44