This file contains hidden or 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
func fetchData(url: String) async throws -> Data { | |
guard let requestURL = URL(string: url) else { | |
throw NetworkError.invalidURL | |
} | |
let (data, _) = try await URLSession.shared.data(from: requestURL) | |
guard !data.isEmpty else { | |
throw NetworkError.noData | |
} | |
return data | |
} |
This file contains hidden or 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
sil_stage canonical | |
import Builtin | |
import Swift | |
import SwiftShims | |
import Observation | |
class ViewModel { | |
@_hasStorage @_hasInitialValue private var _name: String { get set } |
This file contains hidden or 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
// | |
// LazyState.swift | |
// Extensions | |
// | |
// Created by Michael Long on 3/5/25. | |
// | |
import Observation | |
import SwiftUI |
This file contains hidden or 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
// present | |
@available(iOS 26.0, macOS 26.0, tvOS 26.0, watchOS 26.0, visionOS 26.0, *) | |
#available(iOS 26.0, macOS 26.0, tvOS 26.0, watchOS 26.0, visionOS 26.0, *) | |
// past | |
@available(iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) | |
#available(iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) | |
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *) | |
#available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *) |
This file contains hidden or 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
// demonstrate advantages of moving task from view to loading state | |
struct FeedView: View { | |
@Environment(BlueSkyClient.self) private var client | |
@Environment(AppTheme.self) private var theme | |
enum ViewState { | |
case loading | |
case error(String) | |
case loaded([Post]) |
This file contains hidden or 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
// | |
// ContentView.swift | |
// ConditionalViews | |
// | |
// Created by Michael Long on 5/28/25. | |
// | |
import SwiftUI | |
This file contains hidden or 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
@MainActor | |
class Test { | |
// can do this | |
func someTask1() { | |
let _ = "" | |
Task { | |
let _ = "" | |
let data = await doBackgroundTask() | |
let _ = data // Update UI with data | |
} |
This file contains hidden or 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
class MyViewModel: ObservableObject { | |
@Published var items: [Item] = [] | |
@MainActor | |
func loadData() async { | |
items = await fetchData() | |
} | |
} | |
struct MyView: View { |
This file contains hidden or 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
// 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)) |
This file contains hidden or 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
// 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 { |
NewerOlder