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 { |
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
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 | |
} |
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 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) |
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
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 | |
} |
NewerOlder