You have to think about threading differently with async/await and the
.task
modifier. Although it begins on the main thread, each line of await code goes off and does something and when it completes it moves on to the next line. The actual thread that the awaited line runs on depends on its actor. If it is an async func inside the View struct then it is the main thread because that is marked with@MainActor
, thus the main thread will be blocked during the await line. If it is declared in a different struct or class then it should be a random background thread. You can also get a background thread within the.task
by using a child Task { } and even if the screen that is showing this View struct dissapears this child Task will still be cancelled same way.task
is, be careful not to useTask.detatched
because that will not be cancelled and could cause a crash. Another way to get a background thread in a View func is to mark it as nonisolated. Put a breakpo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// issue: you are writing a bunch of CGImages to disk concurrently and getting this in the logs: | |
AVEBridge Info: AVEEncoder_CreateInstance: Received CreateInstance (from VT) | |
AVEBridge Error: startUserClient: IOServiceOpen returned 0xe00002e2 | |
Assert - (err == noErr) - f: /AppleInternal/Library/BuildRoots/989643e2-e205-11ee-ae2f-ae2a9a6175d7/Library/Caches/com.apple.xbs/Sources/AppleAVEBridge/AppleAVEEncoder/AppleAVEEncoder.c l: 1955 | |
AVE FIG ERROR: createChannel failed. | |
AVEBridge Error: AVEEncoder_CreateInstance: returning err = -536870174 | |
IMKInputSession (deactivate) timed out waiting for response | |
IMKInputSession (deactivate) timed out waiting for response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AlertCenter.swift | |
// | |
// Created by Gboyega Dada on 03/05/2024. | |
// | |
import Foundation | |
import SwiftUI | |
@Observable class AlertCenter { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples of Different Styles and Syntax | |
# legal / positive case / you can do these stuffs | |
# --------------------------------------------------------------------------- | |
# single line, nothing special | |
Enum.map(0...2, fn i -> IO.puts i end) | |
""" | |
0 | |
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ExampleView.swift | |
// | |
// Created by Gboyega Dada on 13/01/2024. | |
// | |
import SwiftUI | |
struct ExampleView: View { | |
var src: URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TransferableItem.swift | |
// | |
// Created by Gboyega Dada on 22/11/2023. | |
// | |
// @see https://stackoverflow.com/a/57648296/1661299 | |
// @see https://exploringswift.com/blog/creating-a-nsitemprovider-for-custom-model-class-drag-drop-api | |
// @see https://stackoverflow.com/a/66169874/1661299 | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For images... | |
import AppKit | |
extension URL { | |
func getImageSize() -> CGSize? { | |
guard let src = CGImageSourceCreateWithURL(self as CFURL, nil) else { | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct WebImageMetaData: Decodable { | |
let title: String? | |
let summary: String? | |
let url: URL? | |
let image: URL? | |
let mimetype: String? | |
let width: String? | |
let height: String? | |
private enum CodingKeys: String, CodingKey { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on: https://gist.github.com/terranware/962da63ca547f55667f6 | |
var https = require('https'); | |
var util = require('util'); | |
exports.handler = function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
console.log('From SNS:', event.Records[0].Sns.Message); | |
var postData = { |