我有很多正在發送請求的類,最後都是SplitViewController
。在SplitUIviewclass
中,我必須長時間輪詢並在表格視圖中寫入數據。長輪詢在後臺線程中完成,所以我已經在應用程序委託中聲明瞭一個變量,但是當涉及到它時,它是nil
。而問題是每當我試圖通過appdelegate
訪問NSMutablearray
時,它的到來nil
和數組正在被釋放。我對長輪詢代碼如何在每個類中保留弧中的變量
- (void) longPoll {
@autoreleasePool
{
//compose the request
NSError* error = nil;
NSURLResponse* response = nil;
NSURL* requestUrl = [NSURL URLWithString:@"http://www.example.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];
}
//send the next poll request
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
- (void) startPoll {
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
- (void) dataReceived: (NSData*) theData {
//i write data received here to app delegate table
}
如果我把從數據我SPLITVIEW類中的任何其他方法來接收,我失去了控制,我也不能打印接收到的數據或變量我的應用程序委託值被釋放,我無法從這裏調用重載表或任何其他方法。
設置你的財產強/保留 – codercat
你可以在dataRecieved:方法中打印「theData」?這是對的嗎?如果是這樣你需要發佈更多的代碼。 – Ievgenii
我可以在dataRecieved中打印theData,並確定給我一些時間生病d完成問題 – BackStabber