Skip to content

Instantly share code, notes, and snippets.

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
await self.heavyCall()
}
}
import UIKit
class ViewModel {
func heavyCall() {
var count = 0
while count < 1_000_000_000 {
count += 1
}
print(Thread.isMainThread) // false
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
self.heavyCall()
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
await self.heavyCall()
}
}
// ref: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency/#Defining-and-Calling-Asynchronous-Functions
func generateSlideshow(forGallery gallery: String) async {
let photos = await listPhotos(inGallery: gallery)
for photo in photos {
// ... render a few seconds of video for this photo ...
await Task.yield()
}
}
import Foundation
// ref: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/#Property-Wrappers
@propertyWrapper
struct TwelveOrLess {
private var number = 0
@MainActor
var wrappedValue: Int {
get { return number }
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
let y = Y()
await y.f()
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.detached {
let y = Y()
y.f()
}
import UIKit
class ViewController: UIViewController {
var callback: () -> Void = { @MainActor in
print(Thread.isMainThread) // true or false?
}
override func viewDidLoad() {
super.viewDidLoad()
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Task.init {
try await sleep() // 何秒かかる?
}
}