Post

Replies

Boosts

Views

Activity

Reply to scrollTargetLayout + @FetchRequest causes items to constantly re-initialize
Found the cause and fix, though I don't 100% understand why it's happening. The views in the VStack (PostView in the code above) contained a @SceneStorage property. Removing the @SceneStorage allows the list to scroll quickly, even with .scrollTargetLayout(). No excessive calls to .body. I'm guessing that as the scrolled views come and go, that the @SceneStorarge var was getting marked as changed, even though it wasn't, causing all other cached views to be invalidated? But how does .scrollTargetLayout cause this? Anyway, elevating the @SceneStorage to the containing view, and passing into PostView as a binding, fixed scrolling, and still gives access to that @ScensStorage var.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’24
Reply to SwiftData on iOS 18 extreme memory use
@Gil and @max_us I just gave up on SwiftData and reverted to Core Data. A pretty painful few days on a large app, but in the end it runs great on the fall OS's, and I have the convenience of FetchedResultsController for non-view code. I have a gut feeling this won't be fixed in the fall releases.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’24
Reply to SwiftUI/SwiftData view deep in the hierarchy looses model container
@malc Interesting idea. I don't think I'm initing twice. A SwiftData engineer acknowledged the corrupted modelContext environment variable as a known bug. Container initialized in a singleton... @MainActor struct DataCenter { static var shared:DataCenter = DataCenter.init() public let container: ModelContainer public let context: ModelContext init() { let schema = Schema([ Folder.self, Reminder.self, DateReminder.self, LocationReminder.self ]) container = try! ModelContainer(for: schema, configurations: [ModelConfiguration(isStoredInMemoryOnly: false)]) context = container.mainContext } } Setting the container... WindowGroup { ContentView() } .modelContainer(DataCenter.shared.container) I don't reference the modelContext environment variable anywhere. In code that needs the context, I always reference the singleton... let folder = Folder(name: newFolderName) DataCenter.shared.context.insert(folder) It's been working fine, until iOS 18 came along. But I think my many problems with SwiftData in 18 have nothing at all to do with this container usage. But I've been wrong before.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’24
Reply to SwiftData on iOS 18 extreme memory use
Ziqiao, thanks for the fetchCount workaround. Unfortunately I've found another case. Simply iterating over a @Query with ForEach causes the same problem. No call to count. Your suggestion to make large items in externalStorage reference instead does help in this case too. But it points to a serious problem in SwiftData, where Query macros become useless in non-trivial apps. (I'm pretty sure that the problem does not happen when iterating or counting an array representing a one-to-many relationship. Hard to be sure because I can't get the app running well enough to exercise in iOS 18) I do hope that it's fixable, because this is not how SwiftData is supposed to work, and everything works fine on iOS 17.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’24
Reply to ModelContext autosaveEnabled leading to crashes when SwiftUI App moves to background?
This seems a lot like a bug that was introduced to SwiftData around the 17.4 timeframe. The modelContext environment variable becomes corrupted as the app becomes de-active, then when in the background some SwiftData/SwiftUI activity, relying on that variable, crashes. With autosave turned off, that background activity doesn't happen, but you even when that's turned off you'll probably be bitten eventually. The solution is to avoid relying on the modelContext environment variable, instead setting the modelContainer manually on a root view. See these threads for a deeper discussion : https://developer.apple.com/forums/thread/750084?answerId=52256025#52256025 https://developer.apple.com/forums/thread/744194?answerId=788019022#788019022
Aug ’24