i know, this question ask before. But not working for me this scenario. i am working on AFNetworking for transfer layer. My code below.
AFHTTPRequestOperation *post; AFHTTPRequestOperationManager *manager;
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] init] ;
delegate = (id<ConnectionUtilDelegate>) delegateObject;
for (Parameter *param in parameterArray)
{
[paramDict setObject:param.value forKey:param.name];
}
NSDictionary *params = paramDict;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.operationQueue cancelAllOperations];
post = [manager POST:[NSString stringWithFormat:@"%@",url] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[delegate requestCompleted:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[delegate requestFailed:error];
NSLog(@"Error: %@", error);
}];
this is my post request. it's work like charm. But sometimes some request could be late. For instance, i send 2 different post request this name P1(Post 1) and P2(Post 2). if these request response return as a R1(Response 1) and than R2( response 2) is ok for me. But if this request response return as a R2(Response 2) and than R1(Response 1) its a cause for my app. So i want to cancel old request when i send new request. and
[post cancel];
Error: [AFHTTPRequestOperation cancel]: message sent to deallocated instance 0x1723cbf0
not working for me. Do you have any suggestion?
As I see in your code, you create a new AFHTTPRequestOperationManager
instance for each request and then perform cancelAllOperations
over the operationQueue of newly created AFHTTPRequestOperationManager
. I suggest you creating a sharedManager of type AFHTTPRequestOperationManager
and use this singleton object for all your requests:
+ (instancetype)sharedManager {
static AFHTTPRequestOperationManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [AFHTTPRequestOperationManager manager];
});
return instance;
}
Then, you can cancel all requests before performing a new request using:
+ (void)cancelAllRequests {
[[AFHTTPRequestOperationManager sharedManager].operationQueue cancelAllOperations];
}
When you call above method, all requests will fail and failure
block will be called. So you need to check if operation is cancelled
in failure block. And here comes the final code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager sharedManager];
NSMutableDictionary *paramDict = [[NSMutableDictionary alloc] init] ;
delegate = (id<ConnectionUtilDelegate>) delegateObject;
for (Parameter *param in parameterArray)
{
[paramDict setObject:param.value forKey:param.name];
}
NSDictionary *params = paramDict;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
AFHTTPRequestOperation *post = [manager POST:[NSString stringWithFormat:@"%@",url] parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[delegate requestCompleted:responseObject];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (!operation.cancelled) {
[delegate requestFailed:error];
}
}];
[post start];