Hides warnings coming from calling main actor isolated things

Updated
Aug 25, 2022 1:32 PM
Created
Aug 24, 2022 4:05 PM
Tags
SwiftConcurrency
Attributes
image

When you call a function required to run on Main-actor by @MainActor attribute, you might get such warnings or errors. Even if that stack runs on main-thread.

❌
Call to main actor-isolated parameter 'receive' in a synchronous nonisolated context

You can solve this issue by wrapping Task { @MainActor in }, but Task will hop (meaning runs on the next event-loop) It might not be a big problem, but it might be a problem in some typical cases, such as performing for UI. while hopping, other UI operations will go on, then the app might get wrong. for example, allowing the double tap to open another screen or wrong view size calculation before data set.

Not sure this problem is the actual problem, anyway I tried to figure out solutions to run synchronously not dispatching if the caller is already on main-thread.

import Foundation

@preconcurrency
@MainActor
@inline(__always)
func thunkToMainActor(_ run: @MainActor () throws -> Void) rethrows {
  assert(Thread.isMainThread)
  try run()
}

image

Study

struct BookDispatchToActor: View {
  var body: some View {
    Button("Action") {
      
			// call each entrypoint method
			
    }
  }
}

private func thunk(_ closure: @escaping @Sendable () -> Void) {
  closure()
}

@preconcurrency
@MainActor
@inline(__always)
func thunkToMainActor(_ run: @MainActor () throws -> Void) rethrows {
  assert(Thread.isMainThread)
  try run()
}

@MainActor
private func runUIOperation() {
  print("run")
}

Case 1

@MainActor
private func main_isolated_task_entrypoint() {
  
  // main-actor-isolated-context here
  
  thunk {
    Task { @MainActor in
      runUIOperation()
    }
  }
}

image

  • entrypoint method is gone from current stack trace - meaning hop happened

Case 2

@MainActor
private func main_isolated_entrypoint() {
  
  // main-actor-isolated-context here
  
  thunk {
    runUIOperation()
  }
}

image

  • entrypoint method is still in current stack trace - executed synchronously (blocking runloop)

Case 3

private func nonisolated_entrypoint() {

  // non-isolated-context here
  
  thunk {
    Task { @MainActor in
      // detaches into main-actor-isolated-context
      
      // can perform this method without awaiting
      runUIOperation()
    }
  }
}

image

  • entrypoint method is gone from current stack trace - meaning hop happened

Related tips

Indicating which context we are

Recently updated notes

πŸ“–
Notes

NameTagsUpdated
just happened
SwiftUI.frameworkSwift
Sep 22, 2022 6:13 PM
Rendering ascii utils
Swift
Sep 22, 2022 8:45 AM
SwiftUI EnvironmentValues - SwiftUI.PropertyList
SwiftSwiftUI.framework
Sep 22, 2022 10:13 AM
Regex example
Sep 21, 2022 1:27 PM
SwiftUI animation - Structural Identity
SwiftSwiftUI.framework
Sep 16, 2022 12:39 PM
Verge v9 migration note
Sep 15, 2022 3:49 AM
Merging Blob, Metaball
Sep 14, 2022 12:28 PM
SwiftUI Testing
Sep 12, 2022 6:33 PM
Swipe up or down prototyping
SwiftSwiftUI.framework
Sep 10, 2022 9:51 AM
Pick an emoji at random
SwiftMemoDevelopmentSwiftScripting
Sep 10, 2022 7:00 AM
flex-wrap in SwiftUI with Layout API
SwiftSwiftUI.framework
Sep 9, 2022 4:11 PM
SwiftUI backports
SwiftSwiftUI.framework
Sep 9, 2022 4:03 PM
Making Slides in SwiftUI
Swift
Sep 8, 2022 6:35 PM
CORS on Firebase
Sep 5, 2022 8:48 AM
swift-async-shell
SwiftBookmark
Sep 4, 2022 12:34 PM
Furniture shops
Bookmark
Sep 4, 2022 11:06 AM
New StateReader syntax
VergeGroup/VergeSwift
Sep 3, 2022 8:07 PM
Swift snippets for scripting
SwiftSwiftScripting
Sep 4, 2022 12:35 PM
Mixpanel note
mixpanel
Sep 2, 2022 3:23 PM
[scrapbook] Trampoline implementation EventEmitter - Verge
Swift
Sep 1, 2022 3:45 PM
apple/swift-book
SwiftBookmark
Aug 31, 2022 4:50 PM
Swift Concurrency γƒγƒΌγƒˆγ‚·γƒΌγƒˆ
SwiftMemoBookmark
Aug 29, 2022 4:59 AM
Check if it is in Task
SwiftConcurrency
Aug 27, 2022 6:11 AM
Behaviors actor annotated type - Swift Concurrency
SwiftConcurrency
Aug 26, 2022 5:23 PM
WidgetKit note
iOSSwift
Sep 15, 2022 1:58 PM
ActivityKit note
iOSSwift
Sep 15, 2022 3:13 PM
Coffee Grinder - VARIA VS3 GRINDER
Coffee
Aug 29, 2022 6:14 AM
Logger template for iOS12+
Swift
Sep 13, 2022 12:35 PM
Like UIScrollView, how to get "rubber banding effect"
Sep 15, 2022 5:43 AM
πŸ› οΈ
Valuable iOS development resources
UIKit.frameworkSwiftSwiftUI.frameworkObjective-CCocoaiOS
Aug 29, 2022 5:53 AM