1

I working on a project where I will use these services combined:

  1. AFNetworking
  2. Google Places API web service
  3. Parse

Trying to follow the best practices mentioned in the AFNetworking Docs :

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass AFHTTPSessionManager, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

So, I have created a singleton networking manager like :

MyAppAPI.h

#import <Foundation/Foundation.h>
#import "AFHTTPSessionManager.h"
#import "AFNetworking.h"

@interface MyAppAPI : AFHTTPSessionManager

+(MyAppAPI *)sharedInstance;

@end

MyAppAPI.m

#import "MyAppAPI.h"
@implementation MyAppAPI

+(MyAppAPI*)sharedInstance
{
    static MyAppAPI* sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       sharedInstance = [[MyAppAPI alloc] initWithBaseURL:[NSURL URLWithString:kROOT_URL]];
    });
    return sharedInstance;
}
@end

where kROOT_URL is "https://maps.googleapis.com/maps/api/place/nearbysearch"

Usage:

  NSDictionary *params = @{@"some_param":@"some_value" };

   [[MyAppAPI sharedInstance] GET: @"/json"
                            parameters:params
                               success:^(NSURLSessionDataTask *task, id responseObject){


     } failure:^(NSURLSessionDataTask *task, NSError *error)
     {

     }];

Now, it works only for Google Places API we service calls.

  1. What if I want to use another web service, how to change the baseURL, if it's not possible what is the best practice to deal with this situation ?
  2. How to use the manager to work with Parse ? any good practice.

I need best practices advises to combine Parse, AFNetworking and Google Places API web service.

Already found : changing AFNetworking baseURL but not helping

Community
  • 1
  • 1
iOSGeek
  • 5,115
  • 9
  • 46
  • 74
  • Is using NSURLConnection an option (i.e., using Cocoa's URL loading system natively)? Just set up a connection for each of your endpoints. Given your requirements, maybe AFNetworking isn't the best choice? – Extra Savoir-Faire Aug 16 '15 at 00:39

2 Answers2

2

Doing what you're doing is perfectly fine. The important idea is that the singleton being suggested by that doc is a singleton per service. A little adjustment in the choice of singleton names might help clarify, so instead of...

// MyAppAPI.h
@interface MyAppAPI : AFHTTPSessionManager

consider that what you've written so far should be called...

// GooglePlacesAPI.h
@interface GooglePlacesAPI : AFHTTPSessionManager

...and include a method like...

- (void)nearbySearch:(NSString *)value
             success:(void (^)(NSURLSessionDataTask *, id))success 
             failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {

    NSDictionary *params = @{@"some_param":value };
    [self GET: @"/json" parameters:params success:success failure:failure];
}

A customer of this class would then say...

[GooglePlacesAPI nearbySearch:@"some value" success:^(NSURLSessionDataTask *task, id responseObject) {
    // handle success
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    // handle failure
}];

Parse gives you the option to use a REST API, and with that you could follow the same pattern to create a singleton for Parse...

// ParseAPI.h
@interface ParseAPI : AFHTTPSessionManager

It's worth mentioning that Parse also provides a full iOS SDK which wraps the network code in a manner similar to AFNetworking, so this approach wouldn't be necessary if you choose the SDK instead of the REST API.

danh
  • 62,181
  • 10
  • 95
  • 136
  • That's really helpful, thank you for taking the time to leave a detailed answer. I Thought about your suggestion, but I tried to post here the question to validate my thoughts, about Parse since it's my first time to deal with it which one do you suggest AFNetworking + REST API or iOS SDK ? just one more question should I put all WS calls (search by name, filter places, get nearby places) inside `GooglePlacesAPI` or inside my Data model class `Business` to respect MVC ? – iOSGeek Aug 16 '15 at 17:15
  • 1
    Sure. Regarding parse REST vs. iOS SDK, I strongly prefer the iOS SDK which handles the networking, object->son->object transformations for parameters and results, persisted local user, push, etc. etc. About the api design, yes, the point of the pattern we're discussing is a singleton that is the client's view of the remote service, so it should provide methods that correspond to those provided by the service (e.g. in the case of google places, search by name, get nearby, etc) – danh Aug 16 '15 at 18:57
1

The simple answer is to not write your own code at all. Use Parse's SDK for anything on Parse and Google's Places SDK for anything needing Places. There are CocoaPods for both. Otherwise, separating the clients into their own singletons in the way to go, as outlined by danh.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
  • Google Places iOS SDK won't allow me to search places so that's why I'm using Google Places API web service. check [my other question](http://stackoverflow.com/questions/32026175/google-places-api-for-ios-vs-google-places-web-service) about iOS API vs Web service API – iOSGeek Aug 16 '15 at 17:19