下,我得到了一個自定義操作中同步請求,看起來像這樣:NSRunLoop凍結在iPad上的應用程序臨iOS10
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *_data, NSURLResponse *_response, NSError *_error)
{
self->data = [_data retain];
self->tempResponse = _response;
self->tempError = [_error retain];
self->done = true;
}];
[task resume];
// wait until the connection has finished downloading the data or the operation gets cancelled
while (!self->done && !self.isCancelled)
{
[[NSRunLoop currentRunLoop] run];
}
此代碼是很老,但已經經歷了多個版本的IOS工作(只更換NSURLConnection的一些點)。現在在iPad Pro上的iOS 10下,此代碼將凍結我的應用程序。如果我將應用程序置於後臺並重新打開它,它將再次運行。另外,如果我在[task resume]
和self->data = [_data retain];
上放置斷點,則根本不會發生凍結。
我發現修復它裏面的代碼的一種方式,通過添加NSLog的運行循環:
while (!self->done && !self.isCancelled)
{
NSLog(@"BLAH!");
[[NSRunLoop currentRunLoop] run];
}
這吃頗有些性能,從長遠來看無助,因爲所有NSLogs被刪除用於發佈配置。
所以我需要一種方法來解決這個錯誤。也許代碼太老了,有一種新的方式來做到這一點,我不知道。任何幫助表示讚賞。
也許runUntil給予短時間提前運行,直到幫助... – Volker
我試過runUntil:和runMode:beforeDate:已經沒有什麼工作:( –
一般來說,你可能會更好使用dispatch_semaphore或調度組 –