Let's suppose I have a Core Data model using AFIncrementalStore
, and I have multiple REST API endpoints for retrieving a list of objects of that model. I can override -requestForFetchRequest:withContext:
in AFHTTPClient
like so:
- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
withContext:(NSManagedObjectContext *)context {
NSMutableURLRequest *mutableURLRequest = nil;
if ([fetchRequest.entityName isEqualToString:@"Post"]) {
mutableURLRequest = [self requestWithMethod:@"GET" path:@"/posts/foo" parameters:nil];
}
return mutableURLRequest;
}
In this snippet, I retrieve Post
objects at /posts/foo
, but I also need to retrieve another set from /posts/bar
.
How can I do this? The only solution I see is to make two models: one for foo
and one for bar
, but repeating yourself is lame, and there may be many more API endpoints that get Post
objects for me that I'll need to support. Is there some other approach that I'm missing?
/posts/foo
or/posts/bar
? For example, are those Post objects by different users? Can you inspectfetchRequest
to determine whether you're currently looking forfoo
orbar
posts?fetchRequest.predicate.predicateFormat
and determine that way?fetchRequest
more closely than just looking atentityName
. You can also look atfetchRequest.propertiesToFetch
or possibly other things depending on your data model. You'll still need to send two requests, so just make sure it can tell the difference.requestForFetchRequest:withContext:
method might get really big. You might want to consider a more generic pattern in which you get yourNSManagedObject
subclass, and ask that to return a fetch request.requestForFetchRequest:withContext
as that's part ofAFIncrementalStore
, but I can probably refactor cruft out pretty easily. If you post this as an answer, I can give you the credit for it. Otherwise, I'll answer it myself when I have it fully working later on.