问题
I were looking at RestKit framework and how it works. Then I create simple project that will call Github api and load the data into a tableview - after some tutorials I have read -, regarding loading and mapping from remote it works fine and loaded to the tableview but when when I'm trying to fetch from datastore -offline mode- the app will crash and this is the log:
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
This is fetchFromDataStore method:
- (void)fetchFromDataStore
{
NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath]; //self resourcePath]];
self.repos = [GithubRepo objectsWithFetchRequest:request];
[self.tableView reloadData];
}
Any idea why that's happening since I have created entities for them, you can check the source code from this link:
Source Code
Also, is there any useful examples or tutorials for RestKit caching?
Thanks,
回答1:
Finally, I got answer for it..
The problem is connected to the fact that, when I ask for: NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath]; on myViewController.m, that comes back empty. The reason that happens is because the object mapper has not been configured to correctly handle the core data cache. I'm using:
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern;
while I should be using:
- (void)setObjectMapping:(RKObjectMappingDefinition *)objectMapping forResourcePathPattern:(NSString *)resourcePathPattern withFetchRequestBlock:(RKObjectMappingProviderFetchRequestBlock)fetchRequestBlock;
which lives in the Core data extension of the object mapping. The extra block you provide serves the purpose of deciding which fetch request is required, depending on the parameters passed in the resourcePath (in this case, the user RestKit). In the specific case, I'd want to return a fetch request that looks for repos when the repo's user == "RestKit".
The documentation, although hard to find for this method, is pretty helpful:
/**
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;
来源:https://stackoverflow.com/questions/11756210/restkit-fetchfromdatastore-cache-issue