1

With respect to UIView, Apple has some comments on threading recommendations:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW147

Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself but all other manipulations should occur on the main thread.

Is it generally (un)safe to dealloc a UIView object in a background thread? I'm looking for some supporting documentation in either case.

Tim Reddy
  • 4,340
  • 1
  • 40
  • 77

2 Answers2

1

Yes, UIKit objects need to dealloc on the main thread. It is not safe to dealloc a UIView object in a background thread. See here for more details.

Community
  • 1
  • 1
Gabriele Mondada
  • 497
  • 4
  • 14
  • There actually seems to be some hacky code inside UIView/UIViewController to ensure dealloc always gets called on the main thread, even if the last reference is released from a background thread. I wrote some test cases to verify this myself. See this discussion for reference: https://forums.swift.org/t/deinit-and-mainactor/50132/11 – Werner Altewischer Dec 05 '22 at 11:14
0

iOS Developer Library docs link to NSView Restrictions section so I guess they apply also to UIView.

NSView Restrictions

The NSView class is generally not thread-safe. You should create, destroy, resize, move, and perform other operations on NSView objects only from the main thread of an application. Drawing from secondary threads is thread-safe as long as you bracket drawing calls with calls to lockFocusIfCanDraw and unlockFocus.

Source: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-123427

I hope this helps.

Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143