Replies: 2 comments 2 replies
-
If your state is non-optional and not presented in any way then you will need some kind of COW wrapper to move the state onto the heap. Here's a version that will work with @ObservableState - you'll also need to mark the property as @propertyWrapper
public struct ObservedBox<T: Equatable>: Equatable, Observable, Perceptible {
private var ref: Ref<T>
public init(wrappedValue: T) {
self.ref = Ref(wrappedValue)
}
public static func == (lhs: ObservedBox<T>, rhs: ObservedBox<T>) -> Bool {
if lhs.ref === rhs.ref {
return true
} else {
return lhs.wrappedValue == rhs.wrappedValue
}
}
public var wrappedValue: T {
get {
access(keyPath: \.ref)
return ref.val
}
set {
withMutation(keyPath: \.ref) {
if !isKnownUniquelyReferenced(&ref) {
ref = Ref(newValue)
return
}
ref.val = newValue
}
}
}
private let _$perceptionRegistrar = Perception.PerceptionRegistrar()
internal nonisolated func access<Member>(
keyPath: KeyPath<Self, Member>,
file: StaticString = #file,
line: UInt = #line
) {
_$perceptionRegistrar.access(self, keyPath: keyPath, file: file, line: line)
}
internal nonisolated func withMutation<Member, MutationResult>(
keyPath: KeyPath<Self, Member>,
_ mutation: () throws -> MutationResult
) rethrows -> MutationResult {
try _$perceptionRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi @3a4oT, as Luke mentioned, typically the fix for these kinds of problems is that COW wrappers are automatically inserted at presentation nodes using In your situation, do you genuinely have 25 non-optional fields in your state? Does this mean you have 25 features present at all times? It would seem that there is maybe a domain modeling problem here, and if you solve that I believe you will avoid these stack issues. But, if that truly isn't possible, then the COW wrapper that Luke posted is a good way to do it manually. |
Beta Was this translation helpful? Give feedback.
-
Hey all, we started app migration from ViewStore to
@Observable
. Everything was fine until we started migrating one submodule which is pretty big ( contains child states and has ~25 Scopes). The thing is that it works fine on the simulator and only crashes___chkstk_darwin (stack overflows?)
on a physical device. What I tried so far (without success):indirect
swiftSettings: [.unsafeFlags(["-no-stack-check"])]
to my package target@CoW
property wrapper but it's not playing well with the@Observable
macro (compilation errors)Has anyone else faced this issue recently? Do you have any suggestions?
TCA 1.10.3
PS. From what I have read so far, it's because a physical device has a stack limit of 1MB, and the iOS Simulator on macOS has 8MB. The obvious way to fix it is to shrink Reducer, but it's not always a straightforward way. Are there any tools that can resolve this kind of warning or maybe throw (runtime warnings that your reducer is too big?)
References:
https://forums.swift.org/t/what-is-chkstk-darwin/59519/15
Crash on startup since 0.24.0 #752
Beta Was this translation helpful? Give feedback.
All reactions