我看到了一個關於pastebin的長輪詢技術的例子,我想知道設計的遞歸性質是否會導致堆棧溢出?對不起,如果這是一個noob的問題,但我不熟悉長輪詢,我不是非常熟悉objective-c。這種遞歸長輪詢技術會導致堆棧溢出嗎?
//long polling in objective-C
- (void) longPoll {
//create an autorelease pool for the thread
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//compose the request
NSError* error = nil;
NSURLResponse* response = nil;
NSURL* requestUrl = [NSURL URLWithString:@"http://www.mysite.com/pollUrl"];
NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl];
//send the request (will block until a response comes back)
NSData* responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
//pass the response on to the handler
//(can also check for errors here, if you want)
[self performSelectorOnMainThread:@selector(dataReceived:)
withObject:responseData waitUntilDone:YES];
//clear the pool
[pool drain];
//send the next poll request
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
- (void) startPoll {
//not covered in this example:
// -stopping the poll
// -ensuring that only 1 poll is active at any given time
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
- (void) dataReceived: (NSData*) theData {
//process the response here
}
來源例如: http://pastebin.com/3z5SM4R0
編輯: 如果這是形成不良的問題,值得一downvote的不好的事將有助於阻止我問在貧窮問題快速記事未來。否則,stackoverflow開始感覺像一個不友好的和獨家社區。
它可能不是破壞堆棧,但男孩你好將它催生了大量的線程,這有時非常非常昂貴。 – CodaFi
@CodaFi所以它會爲每次遞歸產生一個新線程? – David
這是一個實現細節。我相信如果有機會,它可能會有,但希望他們變得更聰明並在GCD之上實施它。選擇器已經在後臺執行了,只是將它設置爲延遲並且它將繼續在同一個線程上調用。更好的是,將其重構爲NSOperation並創建一個私有隊列。您將能夠更輕鬆地調試任何故障。 – CodaFi