如何减少数据显示时的响应时间,这是由SOAP服务解析的

时间:2016-12-02 05:40:53

标签: ios objective-c nsurlconnection grand-central-dispatch nsurlsession

  • 我正在尝试解析SOAP数据[使用NSURLConnection,因为我没有使用NSURLSession]
  • 并尝试在多个UILabel上显示

问题是 - 在控制台上打印响应数据时,它会快速打印,但标签上显示的时间太长(50-60秒)。

请建议我快速显示数据的任何可能方法,这是我的代码

#pragma mark - post data function
-(void)getEmployeeData{
    //Renewed CODE
    loginData = [[NSMutableData alloc]init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    NSString *methodName = @"Get_Data";

    //SOAP Start
    [connectionManagerObject getStartSoapData:loginData :methodName];

    //SOAP Body
    NSString *passedUserID = [[NSUserDefaults standardUserDefaults] objectForKey:@"StoredUserID"];
    //    NSLog(@"passed UserID : %@",passedUserID);
    NSString *passedPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"StoredPassword"];
    //    NSLog(@"passed Password : %@",passedPassword);

    NSString *userIdValue = [NSString stringWithFormat:@"<EMP_NAME>%@</EMP_NAME>",passedUserID];
    NSString *passwordValue = [NSString stringWithFormat:@"<EMP_PASSWORD>%@</EMP_PASSWORD>",passedPassword];
    [loginData appendData:[userIdValue dataUsingEncoding:NSUTF8StringEncoding]];
    [loginData appendData:[passwordValue dataUsingEncoding:NSUTF8StringEncoding]];

    //SOAP End
    [connectionManagerObject getEndSoapData:loginData :methodName];

    //SOAP Request
    [connectionManagerObject getRequest:loginData :request :[NSURL URLWithString:HOST_URL] :methodName];

    //Start connection
    [self startConnectionWithRequest:request];
}

#pragma mark - Start Connection
-(void)startConnectionWithRequest:(NSMutableURLRequest *)URLRequest
{
    loginConnection = [[NSURLConnection alloc]initWithRequest:URLRequest delegate:self startImmediately:YES];

    if (loginConnection)
    {
        //        NSLog(@" \n\n Successfully connected with server !!! ");
    }
    else
    {
        //        NSLog(@" \n\n Connection Not Established !!! ");
    }

    [loginConnection start];
}

#pragma mark - Connection Delegate

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [loginData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data
{
    [loginData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning !" message:@"Please Check your internet connection" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];
    [self presentViewController:alertController animated:YES completion:nil];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *responseString = [[NSMutableString alloc]initWithData:loginData encoding:NSUTF8StringEncoding];

    responseString = [responseString substringFromIndex:[responseString rangeOfString:@"<Get_DataResult>"].location + [@"<Get_DataResult>" length]];
    responseString = [responseString substringToIndex:[responseString rangeOfString:@"</Get_DataResult>"].location];

    //    NSLog(@"Response string : %@",responseString);


    if ([responseString containsString:@"^"])
    {
        [self displayProfileData:responseString];
    }
    else
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning !" message:@"Please Check your login credentials" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
        [self presentViewController:alertController animated:YES completion:nil];

        [self.navigationController popViewControllerAnimated:NO];

    }


}


#pragma mark - Display data function

-(void)displayProfileData:(NSString *)storedEmployeeResponse
{
    [NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil];

    //GCD CODE
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{


    // do my time consuming task and everytime it wants to update the UI,
    // it should dispatch that back to the main queue, e.g.

    NSArray *passEmpDataArray = [[NSArray alloc]init];
    passEmpDataArray = [storedEmployeeResponse componentsSeparatedByString:@"^"];

    NSString *empCodeStr = [passEmpDataArray objectAtIndex:0];
    NSString *empNameStr = [passEmpDataArray objectAtIndex:1];
    NSString *empDeptStr = [passEmpDataArray objectAtIndex:3];
    NSString *empDesgStr = [passEmpDataArray objectAtIndex:5];
    NSString *empGenderStr = [passEmpDataArray objectAtIndex:4];
    NSString *empDojStr = [passEmpDataArray objectAtIndex:6];
    NSString *empDobStr = [passEmpDataArray objectAtIndex:7];
    NSString *empEmailStr = [passEmpDataArray objectAtIndex:9];
    NSString *empMobileStr = [passEmpDataArray objectAtIndex:8];



    dispatch_sync(dispatch_get_main_queue(), ^{

    // update the UI accordingly

    _empCodeLabel.text = [NSString stringWithFormat:@"Emp Code    %@",empCodeStr];
    _empNameLabel.text = [NSString stringWithFormat:@"Emp Name   %@",empNameStr];
    _empDeptLabel.text = [NSString stringWithFormat:@"Emp Dept    %@",empDeptStr];
    _empDesgLabel.text = [NSString stringWithFormat:@"Emp Desg   %@",empDesgStr];
    _empGenderLabel.text = [NSString stringWithFormat:@"Gender         %@",empGenderStr];
    _empDojLabel.text = [NSString stringWithFormat:@"DOJ              %@",empDojStr];
    _empDobLabel.text = [NSString stringWithFormat:@"DOB              %@",empDobStr];
    _empEmailIdLabel.text = [NSString stringWithFormat:@"Email ID        %@",empEmailStr];
    _empMobileNo.text = [NSString stringWithFormat:@"Mobile No     %@",empMobileStr];


    NSString *tempUrl = [NSString stringWithFormat:Image_URL,empCodeStr];
    NSURL *url = [NSURL URLWithString:tempUrl];
    loginData = [NSMutableData dataWithContentsOfURL:url];
    _profilePic.image = [UIImage imageWithData:loginData];

    [self stop];

    });

    });

}

#pragma mark - post data function
-(void)getEmployeeData
{

    //Renewed CODE
    loginData = [[NSMutableData alloc]init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    NSString *methodName = @"Get_Data";

    //SOAP Start
    [connectionManagerObject getStartSoapData:loginData :methodName];

    //SOAP Body
    NSString *passedUserID = [[NSUserDefaults standardUserDefaults] objectForKey:@"StoredUserID"];
    //    NSLog(@"passed UserID : %@",passedUserID);
    NSString *passedPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"StoredPassword"];
    //    NSLog(@"passed Password : %@",passedPassword);

    NSString *userIdValue = [NSString stringWithFormat:@"<EMP_NAME>%@</EMP_NAME>",passedUserID];
    NSString *passwordValue = [NSString stringWithFormat:@"<EMP_PASSWORD>%@</EMP_PASSWORD>",passedPassword];
    [loginData appendData:[userIdValue dataUsingEncoding:NSUTF8StringEncoding]];
    [loginData appendData:[passwordValue dataUsingEncoding:NSUTF8StringEncoding]];

    //SOAP End
    [connectionManagerObject getEndSoapData:loginData :methodName];

    //SOAP Request
    [connectionManagerObject getRequest:loginData :request :[NSURL URLWithString:HOST_URL] :methodName];

    //Start connection
    [self startConnectionWithRequest:request];
}
#pragma mark - Start Connection
-(void)startConnectionWithRequest:(NSMutableURLRequest *)URLRequest
{
    loginConnection = [[NSURLConnection alloc]initWithRequest:URLRequest delegate:self startImmediately:YES];

    if (loginConnection)
    {
        //        NSLog(@" \n\n Successfully connected with server !!! ");
    }
    else
    {
        //        NSLog(@" \n\n Connection Not Established !!! ");
    }

    [loginConnection start];
}

#pragma mark - Connection Delegate

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [loginData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data
{
    [loginData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning !" message:@"Please Check your internet connection" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];
    [self presentViewController:alertController animated:YES completion:nil];

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *responseString = [[NSMutableString alloc]initWithData:loginData encoding:NSUTF8StringEncoding];

    responseString = [responseString substringFromIndex:[responseString rangeOfString:@"<Get_DataResult>"].location + [@"<Get_DataResult>" length]];
    responseString = [responseString substringToIndex:[responseString rangeOfString:@"</Get_DataResult>"].location];

    //    NSLog(@"Response string : %@",responseString);


    if ([responseString containsString:@"^"])
    {
        [self displayProfileData:responseString];
    }
    else
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning !" message:@"Please Check your login credentials" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
        [self presentViewController:alertController animated:YES completion:nil];

        [self.navigationController popViewControllerAnimated:NO];

    }


}


#pragma mark - Display data function
-(void)displayProfileData:(NSString *)storedEmployeeResponse
{
    [NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil];

    //GCD CODE
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{


    // do my time consuming task and everytime it wants to update the UI,
    // it should dispatch that back to the main queue, e.g.

    NSArray *passEmpDataArray = [[NSArray alloc]init];
    passEmpDataArray = [storedEmployeeResponse componentsSeparatedByString:@"^"];

    NSString *empCodeStr = [passEmpDataArray objectAtIndex:0];
    NSString *empNameStr = [passEmpDataArray objectAtIndex:1];
    NSString *empDeptStr = [passEmpDataArray objectAtIndex:3];
    NSString *empDesgStr = [passEmpDataArray objectAtIndex:5];
    NSString *empGenderStr = [passEmpDataArray objectAtIndex:4];
    NSString *empDojStr = [passEmpDataArray objectAtIndex:6];
    NSString *empDobStr = [passEmpDataArray objectAtIndex:7];
    NSString *empEmailStr = [passEmpDataArray objectAtIndex:9];
    NSString *empMobileStr = [passEmpDataArray objectAtIndex:8];



    dispatch_sync(dispatch_get_main_queue(), ^{

    // update the UI accordingly

    _empCodeLabel.text = [NSString stringWithFormat:@"Emp Code    %@",empCodeStr];
    _empNameLabel.text = [NSString stringWithFormat:@"Emp Name   %@",empNameStr];
    _empDeptLabel.text = [NSString stringWithFormat:@"Emp Dept    %@",empDeptStr];
    _empDesgLabel.text = [NSString stringWithFormat:@"Emp Desg   %@",empDesgStr];
    _empGenderLabel.text = [NSString stringWithFormat:@"Gender         %@",empGenderStr];
    _empDojLabel.text = [NSString stringWithFormat:@"DOJ              %@",empDojStr];
    _empDobLabel.text = [NSString stringWithFormat:@"DOB              %@",empDobStr];
    _empEmailIdLabel.text = [NSString stringWithFormat:@"Email ID        %@",empEmailStr];
    _empMobileNo.text = [NSString stringWithFormat:@"Mobile No     %@",empMobileStr];


    NSString *tempUrl = [NSString stringWithFormat:Image_URL,empCodeStr];
    NSURL *url = [NSURL URLWithString:tempUrl];
    loginData = [NSMutableData dataWithContentsOfURL:url];
    _profilePic.image = [UIImage imageWithData:loginData];

    [self stop];

    });

    });

}

0 个答案:

没有答案