使用 AFNetworking 的最佳实践

Best practice to use AFNetworking

我不确定这是否是一个有明显答案的问题,但我找不到任何答案。

我正在使用 AFNetworking 连接我的 REST 服务器。 我正在执行基本任务,例如上传和下载图片、发布和获取 json 等

当某些事情发生变化时更新 UI 的最佳做法是什么。例如,如果已成功下载个人资料图片并需要更改表格视图中的图像。

我只有 1 个 class 使用 AFNetworking 我的 APIConnector

APIConnector.h

@interface APIConnector : NSObject

-(void)downloadClientImageToSystem:(NSString *)imageURL;

@end

APIConnector.m
-(void)downloadClientImageToSystem:(NSString *)imageURL{
    //setup
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    //Set url
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",backendURL,imageURL]];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    //Create a download task
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSString *filename = [NSString stringWithFormat:@"%@.jpeg",[[imageURL componentsSeparatedByString:@"&imgIndex="] lastObject]];
        return [documentsDirectoryURL URLByAppendingPathComponent:filename];

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
    {
        if (error) {
            NSLog(@"there was an error downloading profile image");
            [[NSNotificationCenter defaultCenter] postNotificationName:DLImageFail object:self];
        }
        else{
            NSLog(@"File downloaded to: %@", filePath);
            [[NSNotificationCenter defaultCenter] postNotificationName:DLImageSucces object:self];
        }
    }];

    [downloadTask resume];
}

如您所见,目前正在使用 NSNotificationCenter,但这是最佳解决方案吗?我一直在阅读有关代表和区块的内容,但它们似乎都是松散的。我是否应该在需要它的 classes 中实现 AFNetworking,例如我尝试更新 tableview 的 class?

谢谢:)

额外的代码示例

-(void)executePostForURL:(NSString *)url dictionary:(NSDictionary *)dict success:(SuccessBlock)success failure:(FailureBlock)failure{
    [httpManager POST:url parameters:dict progress:nil
              success:^(NSURLSessionDataTask  * _Nonnull task, id  _Nullable responseObject) {
                  //somehow i need to return [responseObject valueForKey:@"updateLabelString"];
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

              }];
}

我正在尝试在 viewdidload 中调用它。这当然只是伪代码并且不起作用,我如何将 [responseObject valueForKey@"updateLabelString"] 值解析为我的 labelToUpdate.text?

-(void)viewDidLoad{
    NSDictionary *dicToSendToServer;
    UILabel *labelToUpdate = @"temp text";

    [apicon executePostForURL:@"serverurl" dictionary:dicToSendToServer success:^(NSString *test){
        labelToUpdate.text = test;
    }failure:nil];
}

我会这样声明:

- (void)executePostForURL:(NSString *)url dictionary:(NSDictionary *)dict success:(void (^)(id objectYouRequested))success failure:(void (^)(NSError *error))failure;

我也喜欢使用 typedef 来避免一些块语法。我通常定义以下内容:

typedef void (^SuccessBlock)(id result);
typedef void (^MySubclassedObjectSuccessBlock)(SubclassedObject *object);
typedef void (^FailureBlock)(NSError *error);

然后将上面的方法声明简化为:

- (void)executePostForURL:(NSString *)url dictionary:(NSDictionary *)dict success:(SuccessBlock)success failure:(FailureBlock)failure;