I working on a project where I will use these services combined:
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.
baseURL
, if it's not possible what is the best practice to deal with this situation ?I need best practices advises to combine Parse, AFNetworking and Google Places API web service.
Already found : changing AFNetworking baseURL but not helping
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.