NSData result causing delay in NSTimer

45 Views Asked by At

I have a NSTimer which should fire its selector after 10 seconds. But due to delay in NSData result, timer is also getting delayed and it fires its selector after NSData operation is performed.

        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                         target:self
                                       selector:@selector(fireTimer)
                                       userInfo:nil
                                        repeats:NO];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:decryptedPath]];
        _audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];

In the above code, Timer waits for NSData result and then fire its selector.

1

There are 1 best solutions below

0
Rob Napier On BEST ANSWER

This is because dataWithContentsOfURL: is blocking the main queue (which is where the timer fires). You should never do that. You need to download this first using NSURLSession, and once it's complete, then pass the data to AVAudioPlayer. AVAudioPlayer is not designed for streaming.