Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:31:08+00:00 2026-06-04T14:31:08+00:00

I have a class with a method (getAndPlaySong) that gets an text file from

  • 0

I have a class with a method (getAndPlaySong) that gets an text file from a server, gets a specific string from the text, and uses the string (a URL) to get a music file from the server. The first server call to get the text file is quick so I implemented it synchronously. The second takes longer and I want to update the display while it is occurring.

I want the method to wait for the asynchronous request to finish before it proceeds. I have tried putting a BOOL in the connectionDidFinishLoading method:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);
    [AppDelegate playAudioWithData: _receivedData];
    _dataIsReceived = YES; //BOOL

    // release the connection, and the data object
    [connection release];
    [_receivedData release];
}

And then putting a while loop in the getAndPlaySong method:

[self startRequest: correctSongURL]; starts the request asynchronously

while (!_dataIsReceived) {
    //do stuff
}

The result is that the app hangs when it reaches the loop. The loop never terminates and I never get the “Succeeded…” output. I am a little confused and would be grateful for some input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:31:10+00:00Added an answer on June 4, 2026 at 2:31 pm

    I don’t wish to sound rude, but what you have done is the worst possible thing. Of course the app hangs because it has an infinite loop. Even if the loop terminates eventually, while its in the loop your app is stuck – what if the user tries to do something with your app while its in the middle of the loop?
    You need to change your mindset and learn how to use event driven programming with a state machine. That’s the paradigm of iOS, you have to knuckle down and do some reading and learning and change the way you think about and construct your programs.

    After you make the request you do nothing, i.e. there is no code coming next in your code snippet. Instead your app sits there doing nothing (but still responsive) waiting for the connection to finish (while your app is doing nothing, the OS has created a thread on your behalf and the connection code is executing inside it, when its finished your connection code needs to inform the rest of your code it has finished, you could do this via a delegate for example, or via a notification).

    And that is not the only thing it needs to sit and wait for – what if the user taps on the screen? What if the user exits your app? What if …
    Your app needs to be able to responde to all possible events and respond quickly, the connection is just one more event it must listen for and respond to. It can’t do that if its in a loop.

    Never use loops anywhere like this.

    “I want the method to wait for the asynchronous request to finish before it proceeds.” No you don’t – you can never do anything like this in an application with a GUI. The function must finish executing. You need to implement a state machine, every iOS is a state machine, the application delegate is the main thing driving the state machine, then you add more states and transitions between the state as more events get added to the program.

    Look at the app delegate and its methods that tell your code the app entered the foreground, or the background, or there is a low memory situation etc. That’s event driven programming, and you need to extend and build on that principle to take into consideration your connection events and any other events.

    Sorry if I sound a bit harsh.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# class method that return a xml document not file. How
I have a class method that looks like this: private List<string> DataStoreContents = new
I have a class method that gets called by a view controller. I want
i have bug that i cannot find, i have Class Point with method who
I have a class with some method that depend by one parameter. What is
I have a class with a method called DataIn(int InputID, string CSVValue) , this
I have a class method that deals with dates: public function setAvailability(DateTime $start, DateTime
I have a class method on User, that returns applies a complicated select /
I have a class FileTransfer that uses an ofstream object, log. The class' constructor
I have class which has a method that needs to return three DataTables. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.