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
struct Account: Identifiable { | |
let id = UUID().uuidString | |
} | |
struct SheetLaunchingDemoView: View { | |
@State var showSheet: Account? | |
var body: some View { | |
NavigationStack { | |
Button("Show Sheet") { | |
showSheet = Account() |
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 | |
} |
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 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") |
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
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) } | |
} |
NewerOlder