2013-05-27 16 views
1

我使用AFNetworking從服務器獲取我的文件。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文件。

我添加了評論//在上面的代碼中執行此操作。我想我需要在這裏執行一些選擇器,以獲取不同的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 
+2

如何簡單地創建一個接受完成處理塊作爲參數的方法的一個版本,不會呢? – Till

+0

我認爲我沒有理解你的權利。你能解釋一下你的觀點嗎? –

+0

我做了系統,其中 - downloadFileWithPath方法獲取塊作爲參數。在我調用這個方法之前,我創建了具有用於獲取另一個文件(.zip)的代碼的塊。我將此塊傳遞給downloadFileWithPath,並等待AFNetworking塊成功完成。在block()之後;現在是工作。我對嗎。清晰的代碼如何是這個好的解決方案呢?我會分享我的代碼。 –

回答

0

感謝這個男人Till

我做這個類下面,解決了我的問題:

#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 

乾淨的代碼或可讀性是什麼?你怎麼看?也許你有建議或意見。

+0

這裏還有一個問題是爲了下載文件。例如,如果我下載文件1.zip和2.zip,它們將以相反的順序下載。首先是2.zip而不是1.zip –

相關問題