Calling web services with asynchImageView

I am having an issue when calling webservices when the image is loaded into a tableView with AsynchImageView files.

Below are the steps for my problem:

  • I am calling a web service and when it returns data, I reload the UITableView and load all the images using the AsynchImageView. The web service returns the URL of the images and some text data.
  • While loading images, if I find the same web service again, it starts up for 30 seconds and then it shuts down without returning anything, but after that it works fine when I call it.

Here is my code for making calls:

-(void)getUserNotificationsPage:(int)page CallBack:(getNotifications)callback{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^ {
       @try {

            getNotificationsArrayResponseCallback=callback;


            NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&page=%@",GET_USER_NOTIFICATIONS,[[NSUserDefaults standardUserDefaults]objectForKey:@"token"],[NSString stringWithFormat:@"%i",page]]]];


            NSError *err;
            NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&err];
            if(err){
                returnData=[NSMutableData data];
            }

            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


   dispatch_async(dispatch_get_main_queue(), ^ {

 id json = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

        NSArray *returnArray=[json objectForKey:@"notifications"];

                getNotificationsArrayResponseCallback(returnArray,YES);

            });

        }
        @catch (NSException *exception) {


            dispatch_async(dispatch_get_main_queue(), ^ {

                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:LANGUAGE(@"Unknown error occurred") message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];


                getNotificationsArrayResponseCallback(nil,NO);

            });
        }        
     });
}

      

  1. If I remove the code where the images are loaded using the asynchImageview, I call the web service call anytime, and the response is fast anytime.

    AsynchImageView *userProfileImageView =[[AsynchImageView alloc] initWithFrameURLStringAndTag:CGRectMake(5, 215, 70, 70) :[NSString stringWithFormat:@"%@%@",SERVER_URL,"some url" ];
    
    [userProfileImageView setBackgroundColor:[UIColor clearColor]];
    
    [userProfileImageView loadImageFromNetwork];
    
    [cell addSubview:userProfileImageView];
    
          

As you can see if I am commenting out the line

[userProfileImageView loadImageFromNetwork];

      

then I can call the web service as many times as I want and the response is fast, but when the asynchimage view loads the images and then I call the service then it will only be disabled for that time. For further service of calls works great.

I think this is an issue with streaming or calling server url requests at the same time.

+3


source to share


2 answers


is this the class you are using? AsynchImageView

If so, it doesn't really look like its NSURLConnection is being processed in the background. This is similar to the main theme.

I have used this other library in the past with AsyncImageView success



If you look in another library, they are using the correct dispatch queue to trigger the background. The other class does not have this.

dispatch_async(dispatch_get_main_queue(), ^(void) {

      

+2


source


Create a custom cell and inside that cell, put this method

-(void)setImageWithUrl:(NSString *)imageUrl
{
    NSURL *urlImage = [NSURL URLWithString:imageUrl];
    [self.loadingIndicator startAnimating];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_async(queue, ^{

        NSError* error = nil;
        NSData *imageData = [NSData dataWithContentsOfURL:urlImage options:nil error:&error];

        if(error){
            dispatch_sync(dispatch_get_main_queue(), ^{

                [self.imgVw setImage:[UIImage imageNamed:@"placeholder.png"]];
                [self.loadingIndicator stopAnimating];
                [[NSURLCache sharedURLCache] removeAllCachedResponses];
            });
        }else{
            UIImage *image = [UIImage imageWithData:imageData];
            dispatch_sync(dispatch_get_main_queue(), ^{

                [self.imgVw setImage:image]; 
                [self.loadingIndicator stopAnimating];
                [[NSURLCache sharedURLCache] removeAllCachedResponses];
            });
        }
    });
}

      



Now call this in the cellForRowAtIndexPath method. Put it in a nil cell state so it doesn't get called every time you scroll. Please comment if in any doubt.

if(custCellObj == nil)
{
      NSString *imgUrl = [yourArray objectAtIndex:indexPath.row];
      [custCellObj setImageWithUrl:imgUrl];
}

      

+2


source







All Articles