realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

SwiftUI warning - publishing changes within a view (Xcode b5)

Open sonisan opened this issue 3 years ago • 11 comments
trafficstars

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

sonisan avatar Aug 11 '22 00:08 sonisan

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.

QuietRocket avatar Aug 16 '22 17:08 QuietRocket

me too +1

drmarkpowell avatar Aug 17 '22 00:08 drmarkpowell

Still happening Xcode 14 beta 6, RealmSwift 10.28.6 macOS Monterey 12.5

drmarkpowell avatar Aug 23 '22 20:08 drmarkpowell

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.

sonisan avatar Aug 24 '22 01:08 sonisan

Still happening Xcode 14 beta 6, RealmSwift 10.28.6 macOS Ventura 13

image image image

robsontenorio avatar Aug 30 '22 15:08 robsontenorio

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.

QuietRocket avatar Sep 13 '22 22:09 QuietRocket

this also affects navigationLink push in iOS16

BugMonkey avatar Sep 14 '22 09:09 BugMonkey

+1

stremblayiOS avatar Sep 15 '22 19:09 stremblayiOS

+1

reinosutisno avatar Sep 17 '22 14:09 reinosutisno

@dianaafanador3 ?

sonisan avatar Sep 17 '22 14:09 sonisan

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.

ejm01 avatar Sep 19 '22 18:09 ejm01

@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/ "

drmarkpowell avatar Sep 28 '22 04:09 drmarkpowell

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.

drmarkpowell avatar Oct 03 '22 16:10 drmarkpowell

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.

mattdornfeld avatar Oct 10 '22 04:10 mattdornfeld

I'm getting this on 14.1 RC2 in Ventura, but I did not get it on RC1 on Monterey.

aehlke avatar Oct 26 '22 23:10 aehlke

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.

drmarkpowell avatar Nov 02 '22 15:11 drmarkpowell

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?

dnlfrst avatar Nov 13 '22 14:11 dnlfrst

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 have the exact same issue with @ObservedResults, also looking for a workaround

agaia avatar Nov 15 '22 09:11 agaia

I have the same issue with @ObservedResults too

JFERSD avatar Nov 16 '22 09:11 JFERSD

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?

ThaC0derDre avatar Nov 16 '22 18:11 ThaC0derDre

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.

ejm01 avatar Nov 16 '22 20:11 ejm01

I don't think this is the right solution. It causes a couple of legitimate testing failures. Needs to be looked into.

ejm01 avatar Nov 17 '22 15:11 ejm01

Same issue waiting for the fix.

santiagoSoft avatar Nov 17 '22 20:11 santiagoSoft

This issue also happend to me Screenshot 2022-11-26 at 9 11 00 PM

X901 avatar Nov 26 '22 18:11 X901

This continues to be an issue in 10.33.0

esummers avatar Dec 07 '22 15:12 esummers

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.

dianaafanador3 avatar Dec 07 '22 17:12 dianaafanador3

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?.

dianaafanador3 avatar Dec 14 '22 19:12 dianaafanador3

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: @.***>

drmarkpowell avatar Dec 14 '22 20:12 drmarkpowell

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 avatar Dec 16 '22 09:12 PhilippeWeidmann

@PhilippeWeidmann can you confirm which property wrappers are you using?

dianaafanador3 avatar Dec 16 '22 09:12 dianaafanador3