我正在使用一组NSURLSessionDownloadTasks
下载多个文件。
我有一个显示进度的UITextView
:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(), ^{
progressTextView.text = [NSString stringWithFormat:@"progress for task %lu: %.2f\n%@", (unsigned long)downloadTask.taskIdentifier, progress, progressTextView.text];
});
}
当应用程序位于前台时,一切正常,但如果我将其发送到后台,则在下载完成后会出现此错误:
*** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /SourceCache/UIFoundation_Sim/UIFoundation-258.1/UIFoundation/TextSystem/NSLayoutManager_Private.m:1510
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
*** First throw call stack:
(
0 CoreFoundation 0x017425e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014c58b6 objc_exception_throw + 44
2 CoreFoundation 0x01742448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x01172720 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 101
4 UIFoundation 0x02d47f81 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 419
5 UIFoundation 0x02d47c66 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2083
6 UIFoundation 0x02d47ce3 -[NSLayoutManager(NSPrivate) _validatedStoredUsageForTextContainerAtIndex:] + 96
7 UIFoundation 0x02d81e28 -[NSLayoutManager usedRectForTextContainer:] + 130
8 UIKit 0x0090a08d -[_UITextContainerView textContainerOrigin] + 97
9 UIKit 0x0090a9b3 -[_UITextContainerView drawRect:] + 146
10 UIKit 0x0029dd56 -[UIView(CALayerDelegate) drawLayer:inContext:] + 504
11 QuartzCore 0x03b4fdc9 -[CALayer drawInContext:] + 123
12 UIFoundation 0x02d7837d -[_UITextTiledLayer drawDirtyLayer:intoContext:] + 171
13 UIFoundation 0x02d78220 -[_UITileLayer drawInContext:] + 64
14 QuartzCore 0x03b4fcfa _ZL16backing_callbackP9CGContextPv + 96
15 QuartzCore 0x03a40cf4 CABackingStoreUpdate_ + 2656
16 QuartzCore 0x03b4fc92 ___ZN2CA5Layer8display_Ev_block_invoke + 93
17 QuartzCore 0x03b83b23 x_blame_allocations + 15
18 QuartzCore 0x03b4fafd _ZN2CA5Layer8display_Ev + 1519
19 QuartzCore 0x03b4fd49 -[CALayer _display] + 33
20 QuartzCore 0x03b4f506 _ZN2CA5Layer7displayEv + 144
21 QuartzCore 0x03b4fd23 -[CALayer display] + 33
22 QuartzCore 0x03b43ed3 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 323
23 QuartzCore 0x03b43f4c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
24 QuartzCore 0x03aabae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
25 QuartzCore 0x03aace71 _ZN2CA11Transaction6commitEv + 393
26 QuartzCore 0x03b69430 +[CATransaction flush] + 52
27 UIKit 0x0026f296 _UIWindowUpdateVisibleContextOrder + 232
28 UIKit 0x0026f145 +[UIWindow _prepareWindowsPassingTestForAppResume:] + 28
29 UIKit 0x00244016 -[UIApplication _updateSnapshotAndStateRestorationArchiveForBackgroundEvent:saveState:exitIfCouldNotRestoreState:] + 187
30 UIKit 0x0024481e __64-[UIApplication _handleBackgroundURLSessionEventWithIdentifier:]_block_invoke + 107
31 UIKit 0x00687877 ___UIAutologgingVoidBlock_block_invoke + 54
32 MyApp 0x000041d0 -[ViewController URLSessionDidFinishEventsForBackgroundURLSession:] + 288
33 CFNetwork 0x062660f0 __67-[__NSCFURLSession delegate_didFinishEventsForBackgroundURLSession]_block_invoke + 49
34 CFNetwork 0x06264286 __37-[__NSCFURLSession addDelegateBlock:]_block_invoke + 133
35 Foundation 0x01141945 -[NSBlockOperation main] + 88
36 Foundation 0x0119a829 -[__NSOperationInternal _start:] + 671
37 Foundation 0x01117558 -[NSOperation start] + 83
38 Foundation 0x0119caf4 __NSOQSchedule_f + 62
39 libdispatch.dylib 0x01aee4b0 _dispatch_client_callout + 14
40 libdispatch.dylib 0x01adc07f _dispatch_queue_drain + 452
41 libdispatch.dylib 0x01adbe7a _dispatch_queue_invoke + 128
42 libdispatch.dylib 0x01adce1f _dispatch_root_queue_drain + 83
43 libdispatch.dylib 0x01add137 _dispatch_worker_thread2 + 39
44 libsystem_pthread.dylib 0x01e7adab _pthread_wqthread + 336
45 libsystem_pthread.dylib 0x01e7ecce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我确保这在主线程上运行,如this answer所示。
到底出了什么问题?