我改變了舊的NSthread
,因爲無法從後臺線程更新UI。現在我正在使用dispatch_async
。while(condition){}通過NSRURLConnection衝突連接嗎?
問題是這個代碼在ApplicationDidBecomeActive中第二次執行時(當用戶隱藏並再次顯示de應用程序時)永遠不會退出並保持無限循環。
在這裏,我在展示結束約dispatch_async新的代碼,該代碼NSURLConnection
總是工作,簡歷...我不認爲這個問題是存在的
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// first connection to the server through NSURLConnection, downloading a json list
[wsDataVideos getVideosData:URL_WS_VIDEOS];
dispatch_queue_t secondDownloadQueue = dispatch_queue_create("name",NULL);
dispatch_async(secondDownloadQueue, ^{
// wait for wsDataVideos has finished. IT SEEMS THIS COLLAPSES CPU ¿?
while (wsDataVideos.imported==NO) {}
// If are there new videos begins sincronous and slower download:
if ....{
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI: inform user begins download
[menuViewController.badgeVideos setBadgeVideosText:@"Downloading..."];
});
// Download videos (sincro)
dispatch_async(dispatch_get_main_queue(), ^{
// informs user is completed
[menuViewController.badgeVideos setBadgeVideosText:@"Downloaded"];
});
}
});
}
第一個連接(JSON),比我在等待,雖然:
-(void)getVideosData:(NSString *)url_ws{
NSLog(@"Get videos data1 ");
if (wsData){
[wsData setLength:0];
}
else{
wsData=[[NSMutableData alloc] init];
}
NSURLRequest *reqCat=[NSURLRequest requestWithURL:[NSURL URLWithString:url_ws] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *conCat=[[NSURLConnection alloc] initWithRequest:reqCat delegate:self];
}
與他們的方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
...
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[wsData appendData:data];
}
...
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
...
任何想法?再次感謝。
什麼設置wsDataVideos.imported爲YES?那行代碼是否被執行過?是getVideosData:在主線程中執行(看起來是),並同步?看起來整個方法會在進入異步塊之前完成。 – shortstuffsushi
第一次連接,通過異步NSURLConnection將wsDataVideos設置爲YES,在connectionDidFinishLoading(和coonectionDidFail..error)中。它在主線程中啓動,並且是異步 –
在此代碼之前,我有NSThread新線程.. [線程啓動]而不是dispatch_async。儘管警告(CoreAnimation:http://stackoverflow.com/questions/19544207/how-to-avoid-coreanimation-warning-deleted-thread-with-uncommitted-catransactio) –