Post

Replies

Boosts

Views

Activity

Reply to Memory leak in refreshable view
you can‘t use .searchable and .refreshable together, Otherwise, a memory leak may occur,use weak can fix this import SwiftUI import Observation struct ContentView: View { @State var path: String = "" var body: some View { NavigationStack { VStack { NavigationLink(value: "new") { Text("Hello, world!") } } .navigationDestination(for: String.self) { _ in SubContentView() } } } } struct SubContentView: View { @State private var viewModel: TestViewModel = TestViewModel(someString: "some string") @State private var searchText: String = "" var body: some View { ScrollView { LazyVStack { Text("\(viewModel.someString)") } } .searchable(text: $searchText) // 删除searchable 后 viewModel 可以正常执行 .refreshable { viewModel.someString = "sadsadsa" } // .refreshable { [weak viewModel] in ✅ // // 同时使用refreshable 和 searchable 的情况下,viewModel必须weak 处理,否则会没法deinit // viewModel?.someString = "sadsadsa" // } .padding() } } @Observable class TestViewModel { var someString: String = "some string" init(someString: String) { self.someString = someString print("init") } deinit { print("deinit") } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’25
Reply to Crash by SwiftData MigarionPlan
I'm facing the same issue as you. When testing on iOS 18 (I don't have a lower version device) using Xcode, everything works fine. However, the app crashes when distributed through TestFlight, and strangely, the second time I try to run it, the crash is gone. The error message indicates a missing critical piece of data. In a moment of desperation, I wrote some seemingly silly code, but surprisingly, it worked, and the TestFlight crash no longer occurs. before: @MainActor class MyModelContainer { @AppStorage(StorageKeys.iCloudSync) public var iCloudSync: Bool = true static let shared = MyModelContainer() let container: ModelContainer private init() { let schema = Schema(versionedSchema: SchemaLatest.self) container = try! ModelContainer(for: schema, migrationPlan: MeMigrationPlan.self, configurations: [ModelConfiguration(isStoredInMemoryOnly: false, cloudKitDatabase: Preferences.shared.iCloudSync ? .automatic : .none)]) } } after: @MainActor class MyModelContainer { @AppStorage(StorageKeys.iCloudSync) public var iCloudSync: Bool = true static let shared = MyModelContainer() let container: ModelContainer private init() { let schema = Schema(versionedSchema: SchemaLatest.self) do { container = try ModelContainer(for: schema, migrationPlan: MeMigrationPlan.self, configurations: [ModelConfiguration(isStoredInMemoryOnly: false, cloudKitDatabase: Preferences.shared.iCloudSync ? .automatic : .none)]) } catch { container = try! ModelContainer(for: schema, migrationPlan: MeMigrationPlan.self, configurations: [ModelConfiguration(isStoredInMemoryOnly: false, cloudKitDatabase: Preferences.shared.iCloudSync ? .automatic : .none)]) } } }
Jul ’24
Reply to TipKit: How to support `concurrency-safe`
@MainActor struct SomeTip: Tip { @Parameter static var haveSome: Bool = false static let eventOneHappened = Event(id: "eventOneHappened") nonisolated var title: Text { MainActor.assumeIsolated { Text("xxx") } } nonisolated var image: Image? { MainActor.assumeIsolated { Image(systemName: "heart") } } nonisolated var options: [TipOption] { MainActor.assumeIsolated { [Tip.MaxDisplayCount(1)] } } nonisolated var rules: [Rule] { MainActor.assumeIsolated { [ #Rule(Self.$haveSome) { $0 == true }, #Rule(Self.eventOneHappened) { $0.donations.count >= 1 } ] } } } I wrote it like this at the beginning. Although there is no warning now, it looks strange. However, my rules are pretty simple, all it takes is control through Event.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24