Skip to content

Instantly share code, notes, and snippets.

View hmlongco's full-sized avatar

Michael Long hmlongco

View GitHub Profile
@MainActor
class Test {
// can do this
func someTask1() {
let _ = ""
Task {
let _ = ""
let data = await doBackgroundTask()
let _ = data // Update UI with data
}
@hmlongco
hmlongco / gist:57181651ecd6a8415d3af3d07182ee21
Created March 29, 2025 18:17
ViewModel called from task
class MyViewModel: ObservableObject {
@Published var items: [Item] = []
@MainActor
func loadData() async {
items = await fetchData()
}
}
struct MyView: View {
@hmlongco
hmlongco / cancellation.swift
Created February 24, 2025 20:10
Task cancellation.swift
// original code, intermediate variable
class SomeViewModel1: ObservableObject {
@Published var searchResults: [String] = []
private var currentSearchTask: Task<Void, Never>?
@MainActor
func search(_ query: String) {
currentSearchTask?.cancel()
let newTask = Task {
do {
try await Task.sleep(for: .milliseconds(500))
@hmlongco
hmlongco / DecodingError.swift
Last active March 10, 2025 18:58
DecodingError.swift
// instead of "nested" try catches...
public func load() async throws -> [FeedItem] {
do {
let (data, response) = try await client.get(from: url)
guard let httpResponse = response as? HTTPURLResponse else {
throw Error.invalidData
}
guard httpResponse.statusCode == 200 else {
@hmlongco
hmlongco / GPT4.swift
Last active April 9, 2024 00:28
GTP4 Form
struct User {
var firstName: String
var lastName: String
var email: String
var phoneNumber: String
var street: String
var city: String
var state: String
var zipCode: String
}
struct Account: Identifiable {
let id = UUID().uuidString
}
struct SheetLaunchingDemoView: View {
@State var showSheet: Account?
var body: some View {
NavigationStack {
Button("Show Sheet") {
showSheet = Account()
@hmlongco
hmlongco / InterruptedTaskView.swift
Created February 25, 2024 23:48
Interrupting a Button
class InterruptedTaskViewModel: ObservableObject {
@Published var name = "Michael"
init() {
print("INIT")
}
@MainActor
func load() {
Task {
print("LOADING")
let _ = try? await Task.sleep(nanoseconds: 3 * NSEC_PER_SEC)
@hmlongco
hmlongco / ClearSO.swift
Created February 25, 2024 15:54
Clearing StateObject with ID
struct ChildStateDemo: View {
@State var account = 1
var body: some View {
VStack(spacing: 20) {
SubView(id: account)
.id(account) // required to change subview state
Button("Account 1") {
account = 1
}
@hmlongco
hmlongco / IDState.swift
Last active February 24, 2024 18:34
ID and State
struct ParentView: View {
@State var id = UUID().uuidString
var body: some View {
VStack(spacing: 20) {
VStack {
Text("Parent View")
Text(id).font(.footnote)
}
ChildView(name: "Child Maintains State On Update")
ChildView(name: "Child Loses State On Update")
@hmlongco
hmlongco / AnyViewTest.swift
Created September 4, 2023 00:22
AnyViewTest code for AnyView article on Medium.
import SwiftUI
class ContentModel: ObservableObject {
@Published var count: Int = 0
let items10 = (0 ..< 10).map { Item(id: $0 + 1) }
let items100 = (0 ..< 100).map { Item(id: $0 + 1) }
let items10K = (0 ..< 10_000).map { Item(id: $0 + 1) }
let items100K = (0 ..< 100_000).map { Item(id: $0 + 1) }
}