2014-01-16 32 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。

因此,考虑将数据存储在适当的位置,可能是核心数据或其他东西。