AFNetworking 3.0 AFURLSessionManager with Cache returns same response with different HTTPBody - Objective C - iOS 10.2

372 Views Asked by At

I'm new on AFNetworking. Directly started with 3.0 version. When I communicating with API, using AFURLSessionManager. My sample code shown below. When I call same request with different HTTPBody response is always same with first call. Searched on SO, but didn't find same issue. Forgive me if asked before.

1. Call = Request URL: A --> HTTPBody: B --> Response: C

2. Call = Request URL: A --> HTTPBody: D --> Response: C

How responses can be same? I think 2nd call shouldn't be C...

Here is my sample code.

@interface DemoApiManager : AFURLSessionManager
@property (strong, nonatomic) AFJSONRequestSerializer* _Nonnull requestSerializer;

- (id)init {
    if (self = [super init]) {
        requestSerializer = [AFJSONRequestSerializer serializer];
    }
    return self;
}

- (void)executeApiRequestCached:(NSString*)jSonString url:(NSString*)url checkCache:(BOOL)checkCache
                     completion:(void(^)(ApiResult* apiResponse, NSError *error))completion{

    Singleton* singleton = [Singleton sharedManager];

    if (checkCache){
        [requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
    }else{
        [requestSerializer setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    }

    NSMutableURLRequest *req = [requestSerializer requestWithMethod:@"POST" URLString:[self prepareUri:url] parameters:nil error:nil];

    [req setTimeoutInterval:10];
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"1.0.0" forHTTPHeaderField:@"accept-version"];
    [req setHTTPBody:[jSonString dataUsingEncoding:NSUTF8StringEncoding]];

    if (singleton.memberToken) {
        [req addValue:singleton.memberToken forHTTPHeaderField:@"token"];
    }

    [[self dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {

            //NSLog(@"Reply JSON: %@", responseObject);
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                ApiResult* result = [[ApiResult alloc] initWithDictionary:responseObject error:&error];
                if (result){
                    completion(result, error);
                }
                else{
                    completion(nil, error);
                }
            }
        } else {
            completion(nil, error);
        }
    }] resume];
}
0

There are 0 best solutions below