我正在尝试以编程方式创建一个 xls 工作表。为了填充工作表,我将多个 NSURLConnection
设置为 100 左右。现在,我的方法是:
- 建立连接并将数据存储到一个数组中。这个数组有 100 个对象。
- 现在获取第一个对象并调用连接。存储数据。并与数组中的第二个对象建立第二个连接。这一直持续到数组中的最后一个对象。
完成 100 个连接平均需要 14 秒。有什么方法可以实现 NSURLConnection
以更快地获得响应吗?
直到昨天,我都遵循如下基本方法:
声明属性:
@property (nonatomic,strong) NSURLConnection *getReportConnection;
@property (retain, nonatomic) NSMutableData *receivedData;
@property (nonatomic,strong) NSMutableArray *reportArray;
初始化viewDidLoad
中的数组:
reportArray=[[NSMutableArray alloc]init];
在按钮操作中初始化 NSURLConnection
:
/initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"****/%@/crash_reasons",ID]];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:tokenReceived forHTTPHeaderField:@"**Token"];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//initialize a connection from request
self.getReportConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
处理接收到的数据:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{
if (connection==_getVersionConnection) {
[self.receivedData_ver appendData:data];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
[JSON[@"app_versions"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (![obj[@"id"] isEqual:[NSNull null]] && ![reportArray_ver containsObject:obj[@"id"]]) {
[reportArray_ver addObject:obj[@"id"]];
}
NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"id"]);
}];
if (JSON!=nil) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Version Reports succesfully retrieved" message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
}
完成后调用另一个连接:
// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection==getReportConnection) {
//check and call the connection again
}
}
今天,我尝试了 NSURLConnection
和 sendAsync
来使用循环一个接一个地触发所有连接,并且效果很好。
self.receivedData_ver=[[NSMutableData alloc]init];
__block NSInteger outstandingRequests = [reqArray count];
for (NSString *URL in reqArray) {
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
[self.receivedData appendData:data]; //What is the use of appending NSdata into Nsmutable data?
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
NSLog(@"login json is %@",JSON);
[JSON[@"app_versions"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (![obj[@"id"] isEqual:[NSNull null]] && ![reportArray_ver containsObject:obj[@"id"]]) {
[reportArray_ver addObject:obj[@"id"]];
}
NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"id"]);
}];
outstandingRequests--;
if (outstandingRequests == 0) {
//all req are finished
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Version Reports succesfully retrieved" message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}];
}
这次完成 100 个请求比旧过程花费了一半的时间,除了 asynReq 之外还有没有更快的方法?使用 NSURLconnection
和 的最佳方案是什么>NSURLConnection 与 asyncReq
?
最佳答案
一些观察:
使用
NSURLSession
而不是NSURLConnection
(如果您支持 iOS 7.0 及更高版本):for (NSString *URL in URLArray) { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; // configure the request here // now issue the request NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // check error and/or handle response here }]; [task resume]; }
如果您绝对必须发出 100 个请求,那么请像您的
sendAsynchronousRequest
实现(或我的dataTaskWithRequest
)那样同时发出它们,而不是按顺序发出。这就是实现巨大性能优势的原因。但是请注意,您无法保证它们会完全按照您发布它们的顺序进行,因此您需要使用一些支持它的结构(例如使用
NSMutableDictionary
或预用占位符填充NSMutableArray
,这样您就可以简单地更新特定索引处的条目,而不是将项目添加到数组中)。最重要的是,请注意,它们可能不会按照要求的顺序完成,因此请确保您妥善处理。
如果您保留 100 个单独的请求,我建议您在非常慢的网络连接上测试它(例如,使用网络链接调节器模拟非常糟糕的网络连接;参见 NSHipster discussion)。只有在连接速度较慢时才会出现问题(超时、UI 问题等)。
我建议使用分派(dispatch)组或操作队列依赖项,而不是递减挂起请求数的计数器。
dispatch_group_t group = dispatch_group_create(); for (NSString *URL in URLArray) { dispatch_group_enter(group); NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; // configure the request here // now issue the request NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // check error and/or handle response here // when all done, leave group dispatch_group_leave(group); }]; [task resume]; } dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // do whatever you want when all of the requests are done });
如果可能,看看您是否可以重构 Web 服务,以便发出一个返回所有数据的请求。如果您正在寻找进一步的性能改进,这可能是实现它的方法(并且它避免了发出 100 个单独请求时涉及的很多复杂性)。
顺便说一句,如果您使用基于委托(delegate)的连接,就像您在原始问题中所做的那样,您应该不在
didReceiveData
中解析数据。那应该只是将数据附加到NSMutableData
。在connectionDidFinishLoading
委托(delegate)方法中执行所有解析。如果您转到基于 block 的实现,这个问题就会消失,但只是对您的代码片段的观察。
关于ios - 处理多个 NSURL 连接的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31343589/