【问题标题】:RestKit - fetchFromDataStore (cache) issueRestKit - fetchFromDataStore(缓存)问题
【发布时间】:2012-07-30 04:04:39
【问题描述】:

我正在研究 RestKit 框架及其工作原理。 然后我创建一个简单的项目,它将调用 Github api 并将数据加载到 tableview 中 - 在我阅读了一些教程之后 - 关于从远程加载和映射它工作正常并加载到 tableview 但是当我试图从数据存储 - 离线模式 - 应用程序将崩溃,这是日志:

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 缓存的有用示例或教程?

谢谢,

【问题讨论】:

    标签: objective-c ios caching core-data restkit


    【解决方案1】:

    终于找到答案了..

    问题与以下事实有关,当我要求: NSFetchRequest *request = [[[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;
    

    它存在于对象映射的核心数据扩展中。您提供的额外块用于决定需要哪个获取请求,具体取决于在 resourcePath 中传递的参数(在本例中为用户 RestKit)。在特定情况下,我希望在 repo 的 user == "RestKit" 时返回一个查找 repos 的 fetch 请求。

    该方法的文档虽然很难找到,但非常有用:

    /**
     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;
    

    【讨论】:

    • 你的 fetchRequestBlock 最终是什么样子的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    相关资源
    最近更新 更多