5

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?

5
  • 1
    How do you decide if you want /posts/foo or /posts/bar? For example, are those Post objects by different users? Can you inspect fetchRequest to determine whether you're currently looking for foo or bar posts? Jul 17, 2013 at 16:54
  • In my case, the API I'm talking to has a screen where I need to request both Foo and Bar Posts to list in a single table view. My post model does have a property indicating which type it is, so perhaps I could look at fetchRequest.predicate.predicateFormat and determine that way?
    – greenisus
    Jul 17, 2013 at 20:46
  • Yeah, it sounds like in your case, you need to inspect fetchRequest more closely than just looking at entityName. You can also look at fetchRequest.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. Jul 17, 2013 at 21:58
  • Also: it sounds like your requestForFetchRequest:withContext: method might get really big. You might want to consider a more generic pattern in which you get your NSManagedObject subclass, and ask that to return a fetch request. Jul 17, 2013 at 22:01
  • That's brilliant! I can't easily change requestForFetchRequest:withContext as that's part of AFIncrementalStore, 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.
    – greenisus
    Jul 17, 2013 at 22:17

1 Answer 1

2
+150

You need to inspect fetchRequest more closely than just looking at entityName. You can also look at fetchRequest.propertiesToFetch or possibly other things depending on your data model. You'll still need to send two requests, so just make sure your AFNetworking subclass can tell the difference.

Also: it sounds like your requestForFetchRequest:withContext: method might get really big. You might want to consider a more generic pattern in which you get your NSManagedObject subclass, and ask that to return a fetch request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.