I am having a problem in calling web services when the image are being loaded in the tableView with AsynchImageView files .
Below are the steps of my problem:
Here is my code for calling services:
-(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);
});
}
});
}
If I remove the code where the images are being loaded with asynchImageview then I call call the web service at any time and the response is fast at any time .
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 comment the line
[userProfileImageView loadImageFromNetwork];
then I can call the web service any number of times and the response is quick but when the asynchimage view is loading the images and then i call the service then it will time out for that time only . For further calling service works fine .
I think this is the issue with threading or calling serveral url requests at the same time .
is this the class you are using? AsynchImageView
If so, it doesn't actually look like his NSURLConnection is being handled in the background. It looks like it's on the main thread.
I have used this other library in the past with success AsyncImageView
If you look in this other library, they are using a proper dispatch queue for running the background. The other class doesn't have that.
dispatch_async(dispatch_get_main_queue(), ^(void) {
Make custom cell and inside of 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 cellForRowAtIndexPath method. Put it inside cell nil condition, so that it does not called everytime you scroll. Make comment in case of any doubt.
if(custCellObj == nil)
{
NSString *imgUrl = [yourArray objectAtIndex:indexPath.row];
[custCellObj setImageWithUrl:imgUrl];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With