SwiftDataTCA
SwiftDataTCA copied to clipboard
UndoManager POC
I'm not sure if you're interested, but I would like to get a proof of concept made for implementing SwiftData's undo/redo functionality in TCA.
I've explored it some so far but it is not easy. Two major problems I've found so far:
- Registering the UndoManager with SwiftData:
- Undo/Redo functionality is off by default, and so far the only way I've seen how to enable it is:
- pass
isUndoEnabled: true
into modelContainer(for:inMemory:isAutosaveEnabled:isUndoEnabled:onSetup:) - then grab the undo manager from
@Environment(\.undoManager)
- then call undoManager.undo()
- pass
- But, right now, we are creating our ModelContainer manually in our Dependency client and none of the initializers for ModelContainer, ModelConfiguration, nor ModelContext have a
isUndoEnabled
param. - Perhaps, we can just create our own UndoManager and attach it to our context, but I've tried that in various ways and have yet to get it working.
- Observability:
- Currently, we're calling
send(.onAppear)
to refresh our data, any time that we ask SwiftData to make a change. But the problem is 1) we have to remember to always callsend(.onAppear)
, and 2) If something else modifies SwiftData (e.g. a sync from CloudKit) then we are unaware and we now have stale data. - If we were using vanilla
@Query
it would handle this for us. I discovered a way to incorporate an@Query
into TCA. Here's a PR. I'm still learning the pros/cons of this approach. - Also, in a few weeks pointfree.co will release their solution for implementing Observable into TCA. So hopefully that should solve observability.