3

I’m downloading multiple files using a set of NSURLSessionDownloadTasks.

I have a UITextView that displays the progress as such:

-(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];
    });
}

It all works fine when the app is in the foreground, but if I send it to the background, I get this error when the downloads finish:

*** 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

I am making sure that this runs on the main thread, as this answer suggested.

What exactly is going wrong?

Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139
  • Is this the complete error message? Please post all details and the stack trace. – CouchDeveloper Dec 19 '13 at 12:42
  • I now added the full stack trace. – Eric Dec 19 '13 at 14:00
  • 1
    This is not the main thread where the exception occurred. – CouchDeveloper Dec 19 '13 at 14:05
  • 3
    See this SO post: http://stackoverflow.com/questions/18974251/app-crashes-after-executing-background-fetch-completionhandler. I think you're basically running into the same problem. The solution (although not accepted) was to call the completionHandler on the main thread (dispatch calling it to the main thread). He gives the rationale for why in the answer. – stuckj May 06 '14 at 20:51
  • Also the reason for exception is stated right in it - "reason: 'Only run on the main thread!'". @stuckj provided a link to an answer which should help. – Timur Kuchkarov Aug 19 '14 at 01:04
  • like other said main thread issue, i had similar problem occur while running separated thread. I covered `dispatch_async(dispatch_get_main_queue(), block);` solved my issue. – Yoon Lee Apr 17 '15 at 00:57

0 Answers0