AFNetworking未能实施

时间:2014-01-31 19:36:16

标签: ios iphone objective-c ipad afnetworking-2

AFHTTPRequestOperationManager有这个实现:

 - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
                                                success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                                failure:(void (^)(AFHTTPRequestOperation         *operation, NSError *error))failure
 {
     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     operation.responseSerializer = self.responseSerializer;
     operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage;
     operation.credential = self.credential;
     operation.securityPolicy = self.securityPolicy;

     [operation setCompletionBlockWithSuccess:success failure:failure];

     return operation;
 }

使用此方法时,成功和失败块永远不会被调用。我把这一行放在实现中之后:

[self.operationQueue addOperation:operation];

它有效。为什么AFNetworking 2.0 AFHTTPRequestOperationManager错过了这一行,或者我只是不明白这种方法?感谢。

1 个答案:

答案 0 :(得分:2)

HTTPRequestOperationWithRequest创建一个操作,但不执行它。将操作添加到使用此调用创建的操作队列时:

[self.operationQueue addOperation:operation];

您实际上是在执行刚刚创建的操作。然后将调用成功和失败块。