Lê Khánh Vinh Lê Khánh Vinh - 3 months ago 18
iOS Question

IOS error in design singleton instance AFNetworking 3

Hi I'm building a singleton instance for

AFNetworking 3.x


my ApiClient.h

@interface ApiClient : AFHTTPSessionManager
+ (ApiClient *)sharedInstance;

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration;


my ApiClient.m

+ (ApiClient *)sharedInstance {
static ApiClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];

});
return sharedInstance;
}


The problem is i cannot initiate an singleton instance

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {

[super initWithBaseURL:url sessionConfiguration:url];
return self;
}


I got 2 error as bellow pic enter image description here

Anyone know. What wrong with my singleton design? Any help is much appreciate! Thanks

PS: i have tried
initwithURL
only is not working without the session configuration

Update. There is an error typo. correct should be

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super initWithBaseURL:url sessionConfiguration:configuration];

if (self) {
// self.responseSerializer = [AFHTTPResponseSerializer serializer];
// self.requestSerializer = [AFHTTPRequestSerializer serializer];
[self setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse)
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse*)proposedResponse.response;
NSMutableDictionary *newHeaders = [[resp allHeaderFields] mutableCopy];
if (newHeaders[@"Cache-Control"] == nil) {
newHeaders[@"Cache-Control"] = @"max-age=120, public";
}

// NSHTTPURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:@"1.1" headerFields:newHeaders];

NSURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:nil headerFields:newHeaders];
NSCachedURLResponse *cachedResponse2 = [[NSCachedURLResponse alloc] initWithResponse:response2
data:[proposedResponse data]
userInfo:[proposedResponse userInfo]
storagePolicy:NSURLCacheStorageAllowed];
return cachedResponse2;
}];
}

return self;
}


It work fine now but whenever I try to call the viewcontroller again. the response serializer is not working properly (2nd call) have incorrect response while 1st call give correct response serializer). an app crash as a result

My strange response for 2nd call for viewcontroller
enter image description here

Rob Rob
Answer

When implementing init method, you want to say self = ..., e.g.

- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
    self = [super initWithBaseURL:url sessionConfiguration:configuration];

    if (self) {
        // do additional configuration here
    }

    return self;
}