开发者

Method Sleep Until Notification

Is there any way that I can have a method sleep until a notification is posted? This is for an asynchronous NS开发者_开发技巧URLConnection. I cannot move to a synchronous connection for multiple reasons.


Methods cannot "sleep"; that only applies to threads. Just split the code that needs to wait out into another method and have that method called when the notification arrives.

- (void) doStuffBeforeConnection {

    [self doPreConnectionStuff];

    NSURL * url = [NSURL URLWithString:@"/U/R/L"];

    NSURLRequest * request = [NSURLRequest requestWithURL:url
                                            cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                         timeoutInterval:0];
    NSURLConnection * conn = [NSURLConnection connectionWithRequest:request
                                                         delegate:self];

    return;
    // We are now "waiting"...
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [self nowDoStuffThatNeededToWait:response];
}


use addobserver and set a target class with selector to be fired on notification. when you need to trigger, use postNotification with notificatonName. there you go..!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜