2014-01-16 34 views
-1

我有很多正在發送請求的類,最後都是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類中的任何其他方法來接收,我失去了控制,我也不能打印接收到的數據或變量我的應用程序委託值被釋放,我無法從這裏調用重載表或任何其他方法。

+3

設置你的財產強/保留 – codercat

+1

你可以在dataRecieved:方法中打印「theData」?這是對的嗎?如果是這樣你需要發佈更多的代碼。 – Ievgenii

+1

我可以在dataRecieved中打印theData,並確定給我一些時間生病d完成問題 – BackStabber

回答

1

廣東話你在ViewControllers設置你的屬性強/保留,像這樣

property (strong,retain) NSMutableArray *myData;

BTW,我學到了剛纔這是不好的做法,用你的AppDelegate作爲全球集裝箱的存儲位置。 ApplicationDelegate是應用程序委託方法和應用程序基礎初始設置的地方;如設置navigationController。

因此,考慮將數據存儲在適當的位置,可能是核心數據或其他東西。