Skip to content

Instantly share code, notes, and snippets.

@gboyegadada
gboyegadada / swift-concurrency-notes.md
Created October 26, 2024 20:15
Swift / SwiftUI Concurrency Notes (see credit at the bottom of each entry)

On .task modifier and the main thread

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 use Task.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

@gboyegadada
gboyegadada / howto.md
Last active October 2, 2024 06:12
How to add a silent audio track to App Store Preview with ffmpeg

How to add a silent audio track to App Store Preview with ffmpeg


Note to self.

ffmpeg -i video-with-no-audio.mp4 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -c:v copy -shortest -map 0:v -map 1:a video-with-stereo-track.mp4
@gboyegadada
gboyegadada / error.log
Created May 4, 2024 08:46
[Swift] CGImageDestinationFinalize Error
// 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
@gboyegadada
gboyegadada / AlertCenter.swift
Created May 3, 2024 08:22
SwiftUI: Alerting your users or waiting for confirmation from a background thread
//
// AlertCenter.swift
//
// Created by Gboyega Dada on 03/05/2024.
//
import Foundation
import SwiftUI
@Observable class AlertCenter {
@gboyegadada
gboyegadada / multiline.exs
Created March 25, 2024 15:27 — forked from squarism/multiline.exs
Multiline Anonymous Functions in Elixir
# 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
@gboyegadada
gboyegadada / ExampleView.swift
Last active January 13, 2024 03:01
Simple GIF player for Swift. Tested on macOS but should work with iOS too (remember to switch NSImage to UIImage in the example)
//
// ExampleView.swift
//
// Created by Gboyega Dada on 13/01/2024.
//
import SwiftUI
struct ExampleView: View {
var src: URL
@gboyegadada
gboyegadada / TransferableItem.swift
Last active June 30, 2024 22:13
[Mac OS] Custom Copy / Paste with Swift UI Transferable + support for NSPasteboard (NSPasteboardWriting, NSPasteboardReading) and drag / drop
//
// 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
//
@gboyegadada
gboyegadada / ImagePixelDimensions.swift
Last active November 14, 2023 13:24
[ Swift for Mac OS ] Load video meta data from URL – duration, natural size and creation date (and more) in Swift. This looks expensive so make sure you store results somewhere for re-use. Bonus: get image pixel dimensions from (local image file) URL!
// For images...
import AppKit
extension URL {
func getImageSize() -> CGSize? {
guard let src = CGImageSourceCreateWithURL(self as CFURL, nil) else {
return nil
}
@gboyegadada
gboyegadada / Decodable.swift
Last active November 5, 2023 06:41
Simple view model for extracting `og:meta` tags from a web page with Swift and WKWebView (without displaying the web view).
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 {
@gboyegadada
gboyegadada / sendCodePipelineApprovalRequestToSlack.js
Last active February 21, 2018 15:47
Send approval request from AWS CodePipeline to Slack using SNS and Lambda
// 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 = {