在 AFNetworking 中调用 Objective C 代码时完成处理程序不工作
Completion handlers not working when calling Objective C code in AFNetworking
我有来自 swift 3 的以下代码,它调用 AFNetworking 代码
typealias StartSessionTaskHandler = (_ task: URLSessionDataTask) -> Void
typealias RetrySessionTaskHandler = (_ newTask: URLSessionDataTask) -> Void
typealias SuccessResponseHandler = (_ task: URLSessionDataTask, _ result: AnyObject) -> Void
typealias FailureResponseHandler = (_ task: URLSessionDataTask?, _ error: NSError) -> Void
fileprivate func POST(_ service: AFHTTPSessionManager, URLString: String, parameters: AnyObject?, handleAuthorization: Bool, start: StartSessionTaskHandler?, retry: RetrySessionTaskHandler?, success:SuccessResponseHandler?, failure: FailureResponseHandler?) {
/*
First, prepare the call
*/
print("*^*^*")
print(parameters!)
initiateSessionTaskWithAuthorization(handleAuthorization, failure: failure) {
/*
The session task is successfully initiated. Do the actual API call
*/
let task = service.post(URLString, parameters: parameters, progress: nil, success: success as? ((URLSessionDataTask, Any) -> Void),
failure: {
(task: URLSessionDataTask?, error: Error) in
/*
Try to handle the failure automatically
*/
self.handleFailureForSessionTask(task, error: error as NSError, handleAuthorizationFailure: handleAuthorization, retry: retry, failure: failure,
newTaskRetrieval: {
/*
This closure creates a copy of the original API call, but with the external failure handler (to avoid potential loops). It is used if we need to retry the original API call
*/
return service.post(URLString, parameters: parameters as! [AnyObject], progress: nil, success: success as! ((URLSessionDataTask, Any) -> Void), failure: failure as! ((URLSessionDataTask?, Error) -> Void)? )
})
})
/*
The task is successfully started
*/
if let task = task {//
start?(task)
}
}
}
这将调用 AFnetwroking 中的 POST 函数,该函数定义为
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure];
[dataTask resume];
return dataTask;
}
这在内部调用数据任务和 http 方法,return根据服务的响应成功或失败
定义如下
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
}
return nil;
}
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self dataTaskWithRequest:request
uploadProgress:uploadProgress
downloadProgress:downloadProgress
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(dataTask, error);
}
} else {
if (success) {
success(dataTask, responseObject); }
}
}];
return dataTask;
}
当我尝试 return 返回成功块时,它显示为 NULL,并且无法 return 返回 swift 代码的响应。这不适用于 swift 3。感谢您的帮助。谢谢
方法 initiateSessionTaskWithAuthorization
需要一个完成处理程序,您必须将其传递给 service.post(...success:HERE)
。
我有来自 swift 3 的以下代码,它调用 AFNetworking 代码
typealias StartSessionTaskHandler = (_ task: URLSessionDataTask) -> Void
typealias RetrySessionTaskHandler = (_ newTask: URLSessionDataTask) -> Void
typealias SuccessResponseHandler = (_ task: URLSessionDataTask, _ result: AnyObject) -> Void
typealias FailureResponseHandler = (_ task: URLSessionDataTask?, _ error: NSError) -> Void
fileprivate func POST(_ service: AFHTTPSessionManager, URLString: String, parameters: AnyObject?, handleAuthorization: Bool, start: StartSessionTaskHandler?, retry: RetrySessionTaskHandler?, success:SuccessResponseHandler?, failure: FailureResponseHandler?) {
/*
First, prepare the call
*/
print("*^*^*")
print(parameters!)
initiateSessionTaskWithAuthorization(handleAuthorization, failure: failure) {
/*
The session task is successfully initiated. Do the actual API call
*/
let task = service.post(URLString, parameters: parameters, progress: nil, success: success as? ((URLSessionDataTask, Any) -> Void),
failure: {
(task: URLSessionDataTask?, error: Error) in
/*
Try to handle the failure automatically
*/
self.handleFailureForSessionTask(task, error: error as NSError, handleAuthorizationFailure: handleAuthorization, retry: retry, failure: failure,
newTaskRetrieval: {
/*
This closure creates a copy of the original API call, but with the external failure handler (to avoid potential loops). It is used if we need to retry the original API call
*/
return service.post(URLString, parameters: parameters as! [AnyObject], progress: nil, success: success as! ((URLSessionDataTask, Any) -> Void), failure: failure as! ((URLSessionDataTask?, Error) -> Void)? )
})
})
/*
The task is successfully started
*/
if let task = task {//
start?(task)
}
}
}
这将调用 AFnetwroking 中的 POST 函数,该函数定义为
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
progress:(void (^)(NSProgress * _Nonnull))uploadProgress
success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
{
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure];
[dataTask resume];
return dataTask;
}
这在内部调用数据任务和 http 方法,return根据服务的响应成功或失败 定义如下
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
}
return nil;
}
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self dataTaskWithRequest:request
uploadProgress:uploadProgress
downloadProgress:downloadProgress
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(dataTask, error);
}
} else {
if (success) {
success(dataTask, responseObject); }
}
}];
return dataTask;
}
当我尝试 return 返回成功块时,它显示为 NULL,并且无法 return 返回 swift 代码的响应。这不适用于 swift 3。感谢您的帮助。谢谢
方法 initiateSessionTaskWithAuthorization
需要一个完成处理程序,您必须将其传递给 service.post(...success:HERE)
。