1
votes

I have a timer:

[NSTimer scheduledTimerWithTimeInterval:(float)jpegInterval/1000 target:self selector:@selector(jpegDownloaderSelector:) userInfo:url repeats:YES];

jpegDownloaderSelector perform the fetching of image from url and set it at imageView as below:

-(void) jpegDownloaderSelector:(NSTimer*)timer{
    [self performSelectorInBackground:@selector(jpegDownloader:) withObject:(NSString*)[timer userInfo]];
}

-(void) jpegDownloader:(NSString*)imageUrl{
    dispatch_async(dispatch_get_main_queue(), ^{        
       imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://10.8.9.100:509/jpeg"]];
       imageView.image = [UIImage imageWithData:imgData];
    });
}

I have also set imageView with gestureRecognizer

[imageView addGestureRecognizer:singleTapRecognizer];
[imageView addGestureRecognizer:doubleTapRecognizer];

where the recognizer definitions are:

singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(functoCall:)];
singleTapRecognizer.numberOfTapsRequired = 1;
singleTapRecognizer.numberOfTouchesRequired = 1;

doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(funcToCall2:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;

The problem i have is, when timer starts, and i double tap imageView, double tap recognizer does not response, instead single tap responded.

If i set NO for timer repeats parameter, double tap responded. Thus, i suspect double tap is not responding due to running of background functions that is called by timer.

Anyone has suggestion that i can implement the background function and no problem with recognizing double tap.

1
I suspect it is because you have such a low repeat interval in your timer. What is jpegInterval? - borrrden
jpegInterval is 200, so the timer will repeat itself every 200ms - Sasa
It will also promptly kill your UI since you just dispatch it back to the main queue anyway. They are not really running in the background. - borrrden
is there anyone has a better solution to this? even using ans suggested by Wain, the jpegInterval need to be set higher. I need it to be as min as possible. - Sasa
Dude, you are gonna be limited by how fast you can access the URL and download the content. This is already the best solution. - borrrden

1 Answers

2
votes

When you do this:

dispatch_async(dispatch_get_main_queue(), ^{

You're pushing the task on to the main thread, just after a very short delay. So your image download is done on the main thread and is blocking all other interaction.

Instead, you should run the download on a background thread (dispatch_get_global_queue) and then push back to the main thread ONLY to handle the result.


-(void) jpegDownloader:(NSString*)imageUrl {
    dispatch_async(dispatch_get_global_queue(), ^{        
       imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://10.8.9.100:509/jpeg"]];

       dispatch_async(dispatch_get_main_queue(), ^{
           imageView.image = [UIImage imageWithData:imgData];
       });
    });
}