0
我的应用程序有大约10到30个webservices(包括get/post)。以前我使用过Async NSURLconnection。大约需要5秒钟才能执行所有服务。但是现在我已经取代了NSurlsession,与nsurlconnection相比,延迟响应延迟了4到6倍。这里我附上了一个webservice方法代码供参考。像下面我有所有方法的代码。NSURLSession执行性能问题
例NSURLsession代码
#pragma mark - NSURLSession
- (NSURLSession *)createSession
{
NSLog(@"DSCReturnAPI: %s",__func__);
static NSURLSession *session = nil;
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.timeoutIntervalForRequest = WEBSERVICE_TIMEOUT;
session = [NSURLSession sessionWithConfiguration:sessionConfig
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
return session;
}
#pragma mark - API Callbacks
-(void)getMyData{
@try {
_webServieRequestType = HWDSCReturnGetRequestType;
NSString *countrycode = [BusinessManager sharedInstance].countryCode;//[HWUserDefaults countryCode];
NSString *userid = [BusinessManager sharedInstance].userId;// [HWUserDefaults userId];
NSString *accessToken = [BusinessManager sharedInstance].accessToken;//[HWUserDefaults accessToken];
NSString *deviceId = [HWUserDefaults deviceId];
[HelperCallbacks logText:[NSString stringWithFormat:@"AccessToken %@",accessToken]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:API_DSCRETURN(countrycode,userid)]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:accessToken forHTTPHeaderField:@"Authorization"];
[request setValue:deviceId forHTTPHeaderField:@"deviceId"];
[request setValue:@"ba598025146eff37e26c9150b180a78b" forHTTPHeaderField:@"If-None-Match"];
theSession = [self createSession];
theDataTask = [theSession dataTaskWithRequest:request];
[theDataTask resume];
}
@catch (NSException *exception) {
[ExceptionHandler sendException:exception];
}
}
#pragma mark - NSURLSessionDataDelegate
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data {
NSLog(@"DSCReturnAPI: %s",__func__);
// NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (theSession == session) {
if (_receivedData == nil) {
_receivedData = [[NSMutableData alloc] initWithData:data];
} else {
[_receivedData appendData:data];
}
}
else
{
//NSLog(@"");
}
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
NSLog(@"DSCReturnAPI: %s",__func__);
if (theSession == session) {
[BusinessManager sharedInstance].urlResponseRecieved = NO;
NSLog(@"DSCReturnAPI: httpResponse:%@",response);
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
httpStatusCode =(int) [httpResponse statusCode]; // To track response Code:
NSLog(@"DSCReturnAPI : Http code:%d",httpStatusCode);
NSLog(@"DSCReturnAPI : Headers %@",[httpResponse allHeaderFields]);
_receivedData = [[NSMutableData alloc]init]; }
completionHandler(NSURLSessionResponseAllow);
}
#pragma mark - NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
NSLog(@"DSCReturnAPI: %s",__func__);
NSLog(@"DSCReturnAPI: didFailWithError :error : %@ errorCode: %ld",error.description,(long)error.code);
@try {
if ([self.serviceDelegate conformsToProtocol:@protocol(ServiceMgrProtocol)] && [self.serviceDelegate respondsToSelector:@selector(webServiceDidFailDSCReturnWithError:)]&& error.code != 0) {
[self.serviceDelegate webServiceDidFailDSCReturnWithError:error.localizedDescription];
}
else {
NSLog(@"DSCReturnAPI:SUCCESS");
[self processResponse];
}
}
@catch (NSException *exception) {
[ExceptionHandler sendException:exception];
}
@finally {
if (theSession) {
theSession = nil;
}
_receivedData = nil;
} }
您好,我已经尝试过,也延迟回应 –
然后,必须从服务器端需要一些清理,尝试清除缓存和服务器内存增加。由于NSURLSession是现代可可触摸中仅有的一种,它比AFNetWorking提供了非常快速和可靠的响应:) –
我已经检查过服务器ppl,他们确定缓存,并且它们没有任何缓存。所以简单地重新启动但没有改变 –