Reputation: 69
here what i want to do
i want perform a loop or kind of something like that before making an action so far, i'm doing like this
//check if it's multiplayer mode
if ([PlayerInfo instance].playingMultiplayer == YES)
{
//no cards has been played
//the while and NSRunLoop combination seems harsh
while ([PlayerInfo instance].cardsPlayed == NULL)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
//after the "loop thing" done, execute this method
//take out cards with the broadcasted cardsPlayed, it's event based
[self takeOutCards:[PlayerInfo instance].cardsPlayed];
}
//single player, don't bother this
else
{
//AI logic, select possible best cards
NSArray * bestCards = [playerLogic selectBestMove];
[self takeOutCards:bestCards];
}
this is looks like a bad practice.
by the way, [PlayerInfo instance].cardsPlayed is a variable that broadcasted from server and will be changed frequently. the changes based on user interaction while another user will wait what cards would be played.
in short, what should i do while waiting the broadcasted variable to come? any suggestion? thanks
Upvotes: 0
Views: 930
Reputation: 62676
Your app already has an event loop running, and it should already be idling in between user actions and while the network is being checked for new state. What you want to do is generate an event when the condition is triggered so the app can react.
The simplest way to do this is to post a notification (within the app) when the condition occurs. Something like this:
// just guessing about your PlayerInfo here, and assuming ARC
@property (nonatomic, strong) NSArray *cardsPlayed;
@synthesize cardsPlayed = _cardsPlayed;
// replace the synthesized setter with one that does the set and checks for
// the condition you care about. if that condition holds, post a notification
//
- (void)setCardsPlayed:(NSArray *)cardsPlayed {
_cardsPlayed = cardsPlayed;
// is the condition about an array that's nil or empty? guessing 'either' here...
if (!_cardsPlayed || !_cardsPlayed.count) {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"CardsPlayedDidBecomeEmpty" object:self];
}
}
Then, when initializing the object that cares about the condition (where you proposed that loop in your question)...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(cardsPlayedEmpty:)
name:@"CardsPlayedDidBecomeEmpty" object:nil];
This will cause cardsPlayedEmpty: to be invoked when the condition comes to pass. It should have a signature like this:
- (void)CardsPlayedDidBecomeEmpty:(NSNotification *)notification {
}
EDIT - I think your revised question is that you want to pause before checking server state,. You can do that using performSelector:withObject:afterDelay: ...
- (void)getRemoteState {
NSURLRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// here, handle the response and check the condition you care about
// post NSNotification as above here
}];
}
// now the code you're trying to write ...
if ([PlayerInfo instance].playingMultiplayer == YES) {
// give other players a chance to play, then check remote state
// this will wait 20 seconds before checking
[self performSelector:@selector(getRemoteState) withObject:nil afterDelay:20.0];
Upvotes: 1