1

I have a UITableView with multiple cells that can contain play buttons. Each play button has a UITapGetsureRecognizer that invokes a NSURLConnection to download the audio file. The NSURLConnection is declared inside my AudioPlayer class which extends an NSObject . Right now, when a user pressed play, we begin downloading. Upon the download completing, we play the audio. If the user tries to press the play button of another audio file while a download is in progress, we successfully cancel the NSURLConnection, set the connection to nil, and open a new connection for the requested audio file. This works great.

I have a problem when the user presses play, the download begins, and then the user pressed the back button (on a NavigationBarOverlay). This causes the app to freeze. I have tried to add code inside of UITableView's viewWillDisappear with no luck. I have tried to override the dealloc method of the AudioPlayer function - no luck. I have changed the implementation of the AudioPlayer class to be initiated with a NSURLConnection pointer (instead of being responsible for alloc'ing the connection, give the responsibility to the UITableView)

Here is some examples of what I've tried:

inside UITableView

-(void) viewWillDisappear:(BOOL)animated {
    [audioConnection cancel];
    [messagePlayer stop];
}

and inside of AudioPlayer Object

-(void) dealloc {
    [self.audioConnection cancel];
}

When I pause the debugger when the freeze occurs the call stack looks like this: Thread 1

Queue: com.apple.main-thread
0 semaphore_wait_trap
.....................................................
4 +[NSURLConnection sendSynchronousRequest:returningResponse:error:]
5 +[Utilities serviceAvailable]
6 -[MessageViewController backButtonPressed] //Note: this is my UITableView
7 -[ViewController backButtonPress]
8 -[NavigationBarOverLayView backButtonPressed:]
9 _UIGestureReconginzerSendActions

Does anybody have any ideas? Does anybody have any ideas why NSURL sendSynchronousRequest is in the call stack. I thought when you initiated the connection like so: self.audioConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

it would asynchronously download the file. Am I wrong?

Thank you. Please let me know if you need any more information.

EDIT: It appears this 'freeze' happens for a little more than 1 minute on iOS 7.1 iPhone 4 retina simulator. Then it resumes normal functionality. To be a bit more descriptive, when a user taps the back button, there is a color change on the button to indicate it has been pressed. During the freeze, this button appears to be "pressed" the entire time.

tpdietz
  • 1,358
  • 9
  • 17

1 Answers1

0

I'd assume that the reason sendSynchronousRequest is in the call stack is because the asynchronous loading is implemented using the same code as the synchronous, but calling it on a background thread.

I know this doesn't answer your main question, but at least it could explain that part!

odlund
  • 247
  • 1
  • 5
  • Thank you for your response. I am new to iOS development, I would imagine if the code was being executed on a background thread the function wouldn't appear in Thread 1 (com.apple.main-thread). Again, I am new to iOS, is this expected behavior? – tpdietz Jul 31 '14 at 19:23
  • Good point, maybe this answer can shine some lights on your question [http://stackoverflow.com/questions/8072893/how-can-i-cancel-an-asynchronous-call-through-nsurlconnection-sendasynchronousre] – odlund Jul 31 '14 at 19:41