|
I'm using the code from Paul's article "How to use @EnvironmentObject to share data between views". I just updated to xcode 11.4. This example crashes every time if you click "Increase Score" first followed by "Show Detail View". If I click "Show Detail View" first, it navigates to the view and I can navigate back. If I do that sequence 3, 4, or 5 times it crashes. The error is "Fatal error: No ObservableObject of type UserSettings found. A View.environmentObject(_:) for UserSettings may be missing as an ancestor of this view." This example works as expected, however, in xcode 11.3. Anyone else seeing this issue? I noticed it first in my own project that stopped working and found this simple example from Paul as a testbed. // SceneDelegate class UserSettings: ObservableObject { @Published var score = 0 } class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// ContentView struct ContentView: View { @EnvironmentObject var settings: UserSettings
} struct DetailView: View { @EnvironmentObject var settings: UserSettings
} |
|
|
|
Hi again, After I found this Paul's tweet (https://twitter.com/twostraws/status/1146315336578469888?s=20) I tried to send environment objects to NavigationLink as well. Now it works. It may help you too. It seems it's a bug and I'm really tired on struggling SwiftUI bugs. :( |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
This has been bugging me as well. Everything runs fine in my simulator, but the SwiftUI preview does not work and it's frustrating as that is supposed to be one of the perks of SwiftUI. I get this:
Even though I've passing everything appropriately in the SceneDelegate and in each View Preview |
|
In the
|
|
I have a
This was breaking a number of my View previews and I managed to fix it by instatiating the 'ScreenCoordinator' class in the preview function as follows:
|
|
Did you try to use @StateObject?
Then pass it as an environmentObject:
Note: @StateObject is only available in iOS 14 (SwiftUI 2.0). You can find more info here. If that didn't work, just try to add self when you pass it:
|
|
We are using SwiftUI "2.0". Firebase reported a crash with our app: Fatal error: No ObservableObject of type Observer found. A View.environmentObject(_:) for Observer may be missing as an ancestor of this view.: file SwiftUI, line 0. The user was on iOS 14.01, but I have not been able to reproduce the issue on iOS 14.1 or 14.2. I have not tried 14.01 yet. However as I have seen this error reported in other forums and it seems to be a bug with SwiftUI when environmentObject is not used with a view created by NavigationLink. The recommend solution was just to use environmentObject, but that seems to defeat the purpose of using the environment to store objects in the first place. Does anyone know of any offical Apple bug fixes where this is addressed? If it's been fixed, then I won't worry about it in older iOS releases. Thanks. |
|
Late to this conversation, but believe I ran into something similar, and may have found a solution. Here is the diff on a commit of an example project where the change resulted in the aforementioned crash and error message not occurring. In short, it appears that when the object is inserted into the environment, it should be attached to the NavigationView, and not the child view within. For a somewhat less trite example, from a bare bones project made while reading Practical Core Data that has a pretty common list / detail / edit sheet navigation structure. I effected a similar series of edits – now only inserting the storage provider into the environment once – but I can use it everywhere, including within a view that is presented within a sheet. Being able to use an object inserted into environment as illustrated in these two examples meshes with my understanding of how this feature of SwiftUI is meant to work. What do others think? |
|
My experience matches that of @lcs-rgordon. If the environmentObject is placed on NavigationViews and on Views presented in a sheet. the object is available. Wish this had been written down somewhere. |
|
My experience matches that of @lcs-rgordon. If the environmentObject is placed on NavigationViews and on Views presented in a sheet. the object is available. Wish this had been written down somewhere. |
|
I had the warning,[Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract. It also crashed after navigating 3-4 times. Xcode 12.5.1 After setting .navigationViewStyle(StackNavigationViewStyle()) on NavigationView the crash was not happen anymore. |
|
Then I remember the numbers. If my baby were to be born today, they would be 10 years old when a quarter of the world’s insects could be gone, when 100 million children are expected to be suffering extreme food scarcity. My child would be 23 when 99% of coral reefs are set to experience severe bleaching. They would be 30 – my age now – when 200 million climate refugees will be roaming the world, when half of all species on Earth are predicted to be extinct in the wild. They would be 80 in 2100, when parts of Australia, Africa and the United States could be uninhabitable dll download We are in the middle of a mass extinction, the first caused by a single species. There are 7.8 billion of us, on a planet that scientists estimate can support 1.5 billion humans living as the average US citizen does today. And we know that the biggest contribution any individual living in affluent nations can make is to not have children. According to one study, having one fewer child prevents 58.6 tonnes of carbon emissions every year; compare that with living car-free (2.4 tonnes), avoiding a transatlantic return flight (1.6), or eating a plant-based diet (0.82). Another study said it was almost 20 times more important than any other choice an environmentally minded individual could make. Such claims have been questioned. After all, does a parent really bear the burden of their child’s emissions? Won’t our individual emissions fall as technologies and lifestyles change? Isn’t measuring our individual carbon footprint – a concept popularised by oil and gas multinational BP – giving a free pass to the handful of corporate powers responsible for almost all carbon emissions? The only thing that isn’t up for debate is that we all know that we are living in ways that can’t continue. |
|
@ccalislar note about setting Looks like default navigation view style swaps visible content by extracting a destination from
we will loose access to |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.