我使用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
答案 0 :(得分: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
干净的代码或可读性是什么?你怎么看?也许你有建议或意见。