realm-swift
realm-swift copied to clipboard
SwiftUI warning - publishing changes within a view (Xcode b5)
How frequently does the bug occur?
All the time
Description
Using Xcode beta 5 triggers the following SwiftUI warnings:
[SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.
No warning was present in the previous beta 3.
My project uses Realm Sync with the @AutoOpen property wrapper. Besides the warning, the code is working as intended.
Stacktrace & log output
Warnings lead to the following block of code:
...
@Published var asyncOpenState: AsyncOpenState = .connecting {
willSet {
objectWillChange.send() // <--- HERE
}
}
...
private func asyncOpenForUser(_ user: User) {
asyncOpenState = .connecting // <--- HERE
....
Can you reproduce the bug?
Yes, always
Reproduction Steps
Launch the complete code example from the SwiftUI quickstart on https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/#complete-code
Version
10.28.5
What SDK flavour are you using?
MongoDB Realm (i.e. Sync, auth, functions)
Are you using encryption?
No, not using encryption
Platform OS and version(s)
macOS 12.5
Build environment
Xcode version: 14.0 beta 5
I am experiencing the same warning when using @ObservedResults. Also seems to have no effect on the resulting program, but mentioning "undefined behavior" is slightly worrisome.
The code causing the warning is line 205 in ObservableStoragePublisher.
func send() {
subscribers.forEach {
_ = $0.receive() // <- This one
}
}
It might be worth noting that I am not using any sync features, just a local database.
me too +1
Still happening Xcode 14 beta 6, RealmSwift 10.28.6 macOS Monterey 12.5
In my case, it seemed to be tied to @AutoOpen and @AsyncOpen property wrappers. I refactored my authentication flow without them (using let realm = try await Realm(configuration: configuration) and the warnings disappeared.
Still happening Xcode 14 beta 6, RealmSwift 10.28.6 macOS Ventura 13
Hello. This issue was opened over a month ago. Will it be addressed?
iOS 16 is out now, and developers want to upload/update their Realm apps. If we can at least have a statement that this warning is negligible (if that is that case) it would be very comforting. Thank you.
this also affects navigationLink push in iOS16
+1
+1
@dianaafanador3 ?
I've still been trying to figure out what's happening here. A couple of others have some interesting ideas. The issue seems to be affecting all developers, not just ones using Realm. One interesting theory here (and elsewhere) says SwiftUI4 may intend for developers to not bind published variables...but I think that'd be odd for Apple to do?
If this warning is on purpose, and we're not supposed to bind published variables, I'm still thinking about what our team can do. But my first guess is this an Apple bug, especially given some reports say the warning can be suppressed by changing button styles.
@ericjordanmossman It looks like your intuition may be right on target regarding this issue.
Donny Wals tweet earlier today: "Heck yes! Xcode 14.1 Beta 3 (finally) fixed the "Publishing changes from within view updates is not allowed, this will cause undefined behavior" bug in SwiftUI https://www.donnywals.com/xcode-14-publishing-changes-from-within-view-updates-is-not-allowed-this-will-cause-undefined-behavior/ "
I'm still seeing a bunch of these warnings at runtime with Xcode 14.1 beta 3, so perhaps that means that only a certain type of modification within a view update was addressed.
I'm seeing this issue when defining a @GestureState variable in a class other than the view. The ui functions as expected but this warning appears.
I'm getting this on 14.1 RC2 in Ventura, but I did not get it on RC1 on Monterey.
In our app we're seeing a high number of these purple warnings consistently at runtime using Xcode 14.1 (formerly RC2) on Monterey. Now that 14.1 is final, this is making me more nervous about it.
I am experiencing the same warning when using
@ObservedResults. Also seems to have no effect on the resulting program, but mentioning "undefined behavior" is slightly worrisome.The code causing the warning is line 205 in ObservableStoragePublisher.
func send() { subscribers.forEach { _ = $0.receive() // <- This one } }
I also observe the runtime warning during the development of gitlapp using realm-swift in version 10.32.0. More importantly, Sentry tracked crashes of the app in production for a marginal fraction of users where the stack trace points to this exact line of code causing a fatal error:
Exception Type: EXC_CRASH (SIGABRT)
Crashed Thread: 0
Application Specific Information:
AttributeGraph precondition failure: %s.
> AttributeGraph precondition failure: setting value during update: 89784.
>
Stack overflow in _Z23RLMAddNotificationBlockI10RLMResultsEP20RLMNotificationTokenPT_U13block_pointerFvP11objc_objectP19RLMCollectionChangeP7NSErrorEP7NSArrayIP8NSStringEPU28objcproto17OS_dispatch_queue8NSObject
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x37656abbc __pthread_kill
1 libsystem_pthread.dylib 0x3b7a67850 pthread_kill
2 libsystem_c.dylib 0x3162756a8 abort
3 AttributeGraph 0x36c141510 AG::precondition_failure
4 AttributeGraph 0x36c1256a4 AG::Graph::value_set
5 SwiftUI 0x310a1f870 Attribute.setValue
6 SwiftUI 0x3100b1290 GraphHost.flushTransactions
7 SwiftUI 0x3100e0af8 GraphHost.asyncTransaction<T>
8 SwiftUI 0x3100e618c AttributeInvalidatingSubscriber.invalidateAttribute
9 SwiftUI 0x3100b7f70 AttributeInvalidatingSubscriber.receive
10 SwiftUI 0x31021df7c AttributeInvalidatingSubscriber<T>
11 SwiftUI 0x3100b8b80 SubscriptionLifetime.Connection.receive
12 Combine 0x32ed98784 AnySubscriberBox.receive
13 Combine 0x32ed87a64 AnySubscriber<T>
14 gitlapp 0x20049cb04 [inlined] ObservableStoragePublisher.send (SwiftUI.swift:205)
15 gitlapp 0x20049cb04 [inlined] Sequence.forEach
16 gitlapp 0x20049cb04 ObservableStoragePublisher.send
17 gitlapp 0x20049dac8 ObservableStorage.value.willset (SwiftUI.swift:241)
18 gitlapp 0x2004ad448 ObservableStorage.value.setter
19 gitlapp 0x20049ef0c [inlined] ObservableStorage.value.setter
20 gitlapp 0x20049ef0c ObservedResults.Storage.setupValue (SwiftUI.swift:435)
21 gitlapp 0x2004a0704 ObservedResults.wrappedValue.getter (SwiftUI.swift:519)
[...]
This crash occurred a dozen times, the stack trace is always the same but originates from different views, all of which use @ObservedResults to query Realm. Unfortunately, I am not even able to reproduce the fatal error on my physical device or the simulator running iOS 15 or iOS 16.
Is there any workaround for this?
I am experiencing the same warning when using
@ObservedResults. Also seems to have no effect on the resulting program, but mentioning "undefined behavior" is slightly worrisome. The code causing the warning is line 205 in ObservableStoragePublisher.func send() { subscribers.forEach { _ = $0.receive() // <- This one } }I also observe the runtime warning during the development of gitlapp using
realm-swiftin version 10.32.0. More importantly, Sentry tracked crashes of the app in production for a marginal fraction of users where the stack trace points to this exact line of code causing a fatal error:Exception Type: EXC_CRASH (SIGABRT) Crashed Thread: 0 Application Specific Information: AttributeGraph precondition failure: %s. > AttributeGraph precondition failure: setting value during update: 89784. > Stack overflow in _Z23RLMAddNotificationBlockI10RLMResultsEP20RLMNotificationTokenPT_U13block_pointerFvP11objc_objectP19RLMCollectionChangeP7NSErrorEP7NSArrayIP8NSStringEPU28objcproto17OS_dispatch_queue8NSObject Thread 0 Crashed: 0 libsystem_kernel.dylib 0x37656abbc __pthread_kill 1 libsystem_pthread.dylib 0x3b7a67850 pthread_kill 2 libsystem_c.dylib 0x3162756a8 abort 3 AttributeGraph 0x36c141510 AG::precondition_failure 4 AttributeGraph 0x36c1256a4 AG::Graph::value_set 5 SwiftUI 0x310a1f870 Attribute.setValue 6 SwiftUI 0x3100b1290 GraphHost.flushTransactions 7 SwiftUI 0x3100e0af8 GraphHost.asyncTransaction<T> 8 SwiftUI 0x3100e618c AttributeInvalidatingSubscriber.invalidateAttribute 9 SwiftUI 0x3100b7f70 AttributeInvalidatingSubscriber.receive 10 SwiftUI 0x31021df7c AttributeInvalidatingSubscriber<T> 11 SwiftUI 0x3100b8b80 SubscriptionLifetime.Connection.receive 12 Combine 0x32ed98784 AnySubscriberBox.receive 13 Combine 0x32ed87a64 AnySubscriber<T> 14 gitlapp 0x20049cb04 [inlined] ObservableStoragePublisher.send (SwiftUI.swift:205) 15 gitlapp 0x20049cb04 [inlined] Sequence.forEach 16 gitlapp 0x20049cb04 ObservableStoragePublisher.send 17 gitlapp 0x20049dac8 ObservableStorage.value.willset (SwiftUI.swift:241) 18 gitlapp 0x2004ad448 ObservableStorage.value.setter 19 gitlapp 0x20049ef0c [inlined] ObservableStorage.value.setter 20 gitlapp 0x20049ef0c ObservedResults.Storage.setupValue (SwiftUI.swift:435) 21 gitlapp 0x2004a0704 ObservedResults.wrappedValue.getter (SwiftUI.swift:519) [...]This crash occurred a dozen times, the stack trace is always the same but originates from different views, all of which use
@ObservedResultsto query Realm. Unfortunately, I am not even able to reproduce the fatal error on my physical device or the simulator running iOS 15 or iOS 16.Is there any workaround for this?
I have the exact same issue with @ObservedResults, also looking for a workaround
I have the same issue with @ObservedResults too
I am experiencing the same warning when using
@ObservedResults. Also seems to have no effect on the resulting program, but mentioning "undefined behavior" is slightly worrisome.The code causing the warning is line 205 in ObservableStoragePublisher.
func send() { subscribers.forEach { _ = $0.receive() // <- This one } }It might be worth noting that I am not using any sync features, just a local database.
Same issue.. Still not a peep regarding this from the team?
One solution might be waiting for the observable storage to finish set up before calling objectWillChange.send(). Appears to resolve the warning for the apps I've been working on locally. Feel free to the try branch in testing. Still need to understand all the implications and if it covers all cases.
I don't think this is the right solution. It causes a couple of legitimate testing failures. Needs to be looked into.
Same issue waiting for the fix.
This issue also happend to me

This continues to be an issue in 10.33.0
This is something we are working out at the moment. We want to remove all the SwiftUI warnings that appear because of our property wrappers. I'll have a branch to test out soon.
I have this branch https://github.com/realm/realm-swift/pull/8068 which removes the warnings in both AsyncOpen/AutoOpen and ObservedResults in my sample project, can someone test it on their projects and check if they still are seeing the warning?.
I’ll test it as soon as I can. (Could be a day or 2, depending)On Dec 14, 2022, at 11:53 AM, Diana Perez Afanador @.***> wrote: I have this branch #8068 which removes the warnings in both AsyncOpen/AutoOpen and ObservedResults in my sample project, can someone test it on their projects and check if they still are seeing the warning?.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>
I have this branch #8068 which removes the warnings in both AsyncOpen/AutoOpen and ObservedResults in my sample project, can someone test it on their projects and check if they still are seeing the warning?.
I can confirm that I don't see a warning with your branch 🎉
@PhilippeWeidmann can you confirm which property wrappers are you using?