tags:

views:

40

answers:

1

Hi Friends, Can you please tell me How can I get Control back from NSOperationQueue

Following is my real question which I asked earlier:

Hi friends , I made an app which plays the song on clicking on the image of artist.(see image attached). Each artist image is implemented on button and on clicking this button, a function is being called which first downloads and then plays the song. I passed this method(function) in a thread but problem is that every time when I click on the image of artist(button) new threads starts running and then multiple songs gets started playing concurrently. How can I use "NSOperation and NSOperationQueue" so that only one song will run at a time . Please help.

alt text Thanks in advance

Now I am able to play song by adding them in queue but when songs starts playing i m not able to do anything on my Screen until song is finished or I scroll the table.

A: 

Have the main thread only queue one message. Have the thread performing the operation (playing a song) send a message back to the main thread indicating when it is done. Don't queue any new operations in the main thread until the main thread receives this message back (statefulness).

Your main thread will get control back if the message is sent back to it with the perform selector on main thread method.

hotpaw2
@:hotpaw2. I have already done that. This is my main method. - (void)main { NSLog(@"Added"); song = [[NSData dataWithContentsOfURL:tempURL] retain]; songPreview = [[AVAudioPlayer alloc] initWithData:song error:nil]; [songPreview setDelegate:self]; [objMainViewScreen performSelectorOnMainThread:@selector(playPreviewMethod) withObject:nil waitUntilDone:NO];}
Harsh
Where is your lock to prevent this method from being called again?
hotpaw2
@hotpaw2: Why I need lock? If I lock this function on first click then I will not be able to call this function on second click that means my app will hang until the downloading is completed on first click or the app may start playing multiple songs together.
Harsh