3

I am using AFNetworking.

I would like to change baseURL manually.

+ (CustomHTTPClient*)sharedClient{
static dispatch_once_t pred;
static CustomHTTPClient *_sharedClient = nil;



NSLog(@"ShareClient %@",_sharedClient);
if (!_sharedClient)
{
    _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"serverURL"]]];
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
}
else
{

    NSLog(@"Delete ShareClient %@",_sharedClient);
    _sharedClient = nil;

    _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"serverURL"]]];
    NSLog(@"NEW ShareClient %@",_sharedClient);
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

}


return _sharedClient;
}

Option 2:

+ (CustomHTTPClient*)sharedClient{
static dispatch_once_t pred;
static CustomHTTPClient *_sharedClient = nil;


dispatch_once(&pred, ^{
    _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"serverURL"]]];
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
});

return _sharedClient;
}

Option 1: This will create shareclient everytime. So it will use correct (new) BaseURL all time. Option 2: only called onces so BaseURL will be same all time.

Quation:What will be implication of using option 1 instead of Option 2?

I have also check some other answers but would like to understand(in simple terms) why to use Dispatch_once and how to make dispatch_once get called multiple time?

changing AFNetworking baseURL

Community
  • 1
  • 1
Nitya
  • 849
  • 1
  • 11
  • 25

2 Answers2

4

If you're switching between base URL's it might just be easier to initialise a new manager each time rather than use a shared one. As much of the benefit of using a shared manager is the single static base URL.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

The point of dispatch once is to execute the block only a single time.

Mark Bridges
  • 8,228
  • 4
  • 50
  • 65
  • Thanks Mark!! It should work the way you describe!! However, I must check some other code in subclass of AFJSONRequestSerializer. @interface CustomHTTPRequestSerializer : AFJSONRequestSerializer. Again Thanks! Now I know what should I check!! – Nitya May 14 '14 at 10:13
3

You can add getter method in your class and return different baseURL's. E.g.

+ (CustomHTTPClient*)sharedClient
{
    static dispatch_once_t pred;
    static CustomHTTPClient *_sharedClient = nil;

    dispatch_once(&pred, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"serverURL"]]];
        [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    });

    return _sharedClient;
}

- (NSURL *)baseURL
{
    if (SOMETHING) {
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"serverURL"]];
    } else {
        return [[NSUserDefaults standardUserDefaults]objectForKey:@"anotherServerURL"]];
    }
}
Marius Kažemėkaitis
  • 1,723
  • 19
  • 22