AFNetworking operations will return before completion event if you put them in an operation queue. Check this blog post: http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/
In your case, you need to create an NSOperation subclass similar to the following:
//Header file
@interface LikeOperation : NSOperation
@property (readonly, nonatomic) BOOL isExecuting;
@property (readonly, nonatomic) BOOL isFinished;
+ (instancetype)operationWithCompletionSuccessBlock:(void(^)())onSuccess failure:(void(^)(NSError *anError))onError;
@end
//Implementation file
#import "LikeOperation.h"
typedef void (^SuccessBlock)();
typedef void (^ErrorBlock)(NSError*);
@interface LikeOperation()
@property (readwrite, copy, nonatomic) SuccessBlock onSuccess;
@property (readwrite, copy, nonatomic) ErrorBlock onError;
@property (assign, nonatomic) BOOL isExecuting;
@property (assign, nonatomic) BOOL isFinished;
@property (readwrite, strong, nonatomic) AFHTTPClient *client;
@end
@implementation LikeOperation
static NSString *const kBaseURLString = @"www.example.org";
static NSString *const kURLString = @"www.example.org/like";
- (id)initWithCompletionSuccessBlock:(void (^)())onSuccess failure:(void (^)(NSError *))onError
{
self = [super init];
if (self)
{
self.onSuccess = onSuccess;
self.onError = onError;
}
return self;
}
+ (instancetype)operationWithCompletionSuccessBlock:(void (^)())onSuccess failure:(void (^)(NSError *))onError
{
return [[self alloc] initWithCompletionSuccessBlock:onSuccess
failure:onError];
}
- (void)start
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start)
withObject:nil
waitUntilDone:NO];
return;
}
NSString *key = NSStringFromSelector(@selector(isExecuting));
[self willChangeValueForKey:key];
self.isExecuting = YES;
[self didChangeValueForKey:key];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL *baseURL = [NSURL URLWithString:kBaseURLString];
self.client = [AFHTTPClient clientWithBaseURL:baseURL];
});
NSURL *url = [NSURL URLWithString:kURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [self.client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.onSuccess();
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
self.onError(error);
}];
[operation start];
}
- (void)finish
{
NSString *isExecutingKey = NSStringFromSelector(@selector(isExecuting));
NSString *isFinishedKey = NSStringFromSelector(@selector(isFinished));
[self willChangeValueForKey:isExecutingKey];
[self willChangeValueForKey:isFinishedKey];
self.isExecuting = NO;
self.isFinished = YES;
[self didChangeValueForKey:isExecutingKey];
[self didChangeValueForKey:isFinishedKey];
}
@end
After that, you can put the above operation safely in an NSOperationQueue and set the max concurrent maxConcurrentOperationCount to 1 so that the operations run one after the another. You might also want to explore nsoperation dependencies, as explained in http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html
//Code to initialize the operation queue
self.queue = [[NSOperationQueue alloc] init];
self.queue.name = @"Post data queue";
self.queue.maxConcurrentOperationCount = 1;
//perform like
- (void)like
{
NSOperation *likeOperation = [LikeOperation operationWithCompletionSuccessBlock:^{
} failure:^(NSError *anError) {
}];
[self.queue addOperation:likeOperation];
}