0
- (void) myFunction {

    User *user = [[User alloc] init];
    user.property = @"value";
    [user login:@"username" password:@"pwd" delegate:nil];

    while (NO == user.runLoopEnd) {
        [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    [user release];

    ...
}

I came across above code and didn't understand the use of While loop. Can anyone explain purpose of While loop in above code?

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177

1 Answers1

0

Secondary threads will die after it executes the assigned task/method. While loop is used to keep the thread alive till our condition is met.

It seems like myFunction method is executed in secondary thread. While loop is used to keep the thread alive until user.runLoopEnd is set to TRUE. Runloop periodically checks the user.runLoopEnd condition and continues to in loop till it is set.

Here you would want to keep the secondary thread alive till the login method comes back with a response. (I guess login method uses a asynchronous api call). After login api returns, it sets the user.runLoopEnd to TRUE and while loop will be broken. So thread can terminate as it completes the method execution.

Dhanaraj
  • 987
  • 1
  • 10
  • 26