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,他們確定緩存,並且它們沒有任何緩存。所以簡單地重新啓動但沒有改變 –