我有两个方法调用都使用dataTaskWithRequest调用。这些方法被背靠背调用。如果第一种方法没有错误,则调用第二种方法。但这非常缓慢,有时可能需要2分钟才能从Web服务获取数据并插入数据。当我在浏览器中手动转到webservice URL时,它非常快。但这只是缓慢的。以下是我的方法:
-(void)barcodeCheck:(NSString *)barcode deviceid:(NSString *)deviceId Completetion:(void (^) (NSArray * result,NSError * error))completion{
NSString *deviceRequestString = [NSString stringWithFormat:@"%@?tagId=%@&deviceId=%@",webservice,barcode, deviceId];
NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data == nil){
completion(nil,error);
return;
}
NSError *myError;
NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
completion(tableArray,myError);
}];
[dataTask resume];
}
-(void)insertTracking:(NSString *)tag deviceId:(NSString *)phone latitude:(double)latitudeId longitude:(double)longitudeId Completetion:(void (^) (NSArray * result,NSError * error))completion{
NSString *deviceRequestString = [NSString stringWithFormat:@"%@?tag=%@&phoneId=%@&latitude=%.10f&longitude=%.10f",webservice,tag, phone, latitudeId, longitudeId];
NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data == nil){
completion(nil,error);
return;
}
NSError *myError;
NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
completion(tableArray,myError);
}];
[dataTask resume];
}
以下是我如何称呼他们:
[self barcodeCheck:barcode deviceid:[[UIDevice currentDevice] name] Completetion:^(NSArray *result, NSError *error) {
if([result count] == 0){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Barcode is invalid." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
ScannerMessage.text = @"READY TO SCAN";
ScannerMessage.backgroundColor = [UIColor greenColor];
}else{
[self insertTracking:barcodeHolder deviceId:[[UIDevice currentDevice] name] latitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude Completetion:^(NSArray *results, NSError *error) {
if([results count] == 0){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Could not insert data" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}else{
ScannerMessage.text = @"SUCCESS";
ScannerMessage.backgroundColor = [UIColor blueColor];
label.text = @"BARCODE";
connectedLabel.text = barcode;
}
}];
}
}];
为什么这么慢?有什么我能做的加速吗?