1

我使用 AFNetworking 从服务器获取我的文件。

我需要使一项操作经常变化。所以下面是关于我所说的代码:

- (void)downloadFileWithPath:(NSString *)urlPath withFileName:(NSString *)fileName
{
    NSURL *url = [NSURL URLWithString:urlPath];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        // DO SOMETHING HERE

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];

    [operation start];
}

所以,我需要什么。首先我需要下载 xml 文件并将其保存到 Documents 目录(我做到了。我不需要解释如何制作)。在这里我调用这段代码:

[object downloadFileWithPath:urlPath withFileName:fileName]

加载 xml 文件后,我需要使用相同的方法调用下一次下载,但在这种情况下,例如另一个文件 .zip。这意味着我需要确定何时加载 xml 文件(在本例中为 setCompletionBlockWithSuccess)并运行下载 .zip 文件。

我在上面的代码中添加了注释// DO SOMETHING HERE。我想我需要在这里为 zip 文件对 xml 文件的不同请求执行一些选择器,因为每次我需要进行不同的操作。所以看起来我们加载了 xml 文件解析器,之后我们获取了 zip 文件的 url 并再次进行下载,但在这种情况下,使用相同的方法获取 zip 文件。

所以我当然可以创建两种不同的方法,例如

- (void)downloadXMLFileWithPath:(NSString *)urlPath withFileName:(NSString *)fileName;  

- (void)downloadZIPFileWithPath:(NSString *)urlPath withFileName:(NSString *)fileName;

在 setCompletionBlockWithSuccess 中具有相同的内容和不同的内容,但我认为这是一个错误的决定,因为重复的代码。

NSURL *url = [NSURL URLWithString:urlPath];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        // BUT DIFFERENT HERE

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];

    [operation start];

那么,您认为哪种变体更好地制作重复代码或将选择器传递给下载文件的方法。

如果我们执行它,选择器也会出现问题,我们会收到以下警告:

PerformSelector may cause a leak because its selector is unknown
4

1 回答 1

0

谢谢这个人直到

我在下面制作了这个类来解决我的问题:

#import "NetworkManager.h"
#import "AFHTTPRequestOperation.h"

typedef void (^NetworkManagerBlock)(void);

@implementation NetworkManager

- (NSString *)fileNameFromUrlString:(NSString *)urlString
{
    NSArray *components = [urlString componentsSeparatedByString:@"/"];
    return [components lastObject];
}

- (void)downloadFileWithUrlString:(NSString *)urlString withCompletionBlock:(NetworkManagerBlock)block
{
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fileName = [self fileNameFromUrlString:urlString];
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        block();

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];

    [operation start];
}

- (void)downloadUpadatesPlistWithUrlString:(NSString *)urlString
{
    NetworkManagerBlock block = ^ {
        [self fetchZipFilesFromUpdatesPlistFile];
    };

    [self downloadFileWithUrlString:urlString withCompletionBlock:block];
}

- (void)fetchZipFilesFromUpdatesPlistFile
{
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString* filePath = [documentsPath stringByAppendingPathComponent:@"updates.plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

    for (NSDictionary *item in [dict objectForKey:@"updates"]) {
        [self downloadZipFileWithUrlString:[item objectForKey:@"url"]];
    }
}

- (void)downloadZipFileWithUrlString:(NSString *)urlString
{
    NetworkManagerBlock block = ^ {
        NSLog(@"ZIP file loaded");
    };

    [self downloadFileWithUrlString:urlString withCompletionBlock:block];
}

@end

干净的代码或可读性如何?你怎么看?也许你有建议或意见。

于 2013-05-28T18:03:50.407 回答