Swift 5.2
I have a NavigationView, for a tree of Views joined by NavigationLinks. (No weirdness going on; no cycles or hopping between different parts of the tree - just views linking to subviews linking to subsubviews.) One of them is a configuration screen. I desire that when you arrive on the config screen (aka ConfigView), the configuration is loaded from disk. In subviews of ConfigView, you can modify different settings, and on ConfigView you can save the settings. However, I desire that if you pop ConfigView (leave, towards the root of the tree), your unsaved changes are discarded.
One of my initial thoughts was to load the changes in the ConfigView initializer. This doesn't work, because ConfigView() is called in the parent view, so exiting the ConfigView and returning to ConfigView keeps the same ConfigView between views (and thus the same dirty data). (...Or, the ConfigView is recreated multiple times without actually leaving it, causing the data to be erroneously reloaded. I tried to test the conditions under which it did either, just now, but ran into a crash I'm not going to fix today. Regardless, I tested it earlier today and it demonstrated one or both of the above behaviors.)
Perhaps load it in the body block? But that gets re-run e.g. when you return from a subview link, which would erroneously reload the data.
Perhaps load it in .onAppear? But that ALSO erroneously reloads the data when you return from a subview.
I considered perhaps going with the dual of my original intent, disposing of the dirty data when you pop the ConfigView (and reloading it whenever requested), but I haven't found any hooks I can use to be notified when e.g. the user hits the "back" button.
How do I get a clean copy of the data to be present on new entry to the ConfigView, and not lost until the ConfigView is popped?
You should have a persistence data somewhere in your app, and when passing data to a child view create a draft data (ObservableObject). Then, save the updated data to the persistence data whenever needed. This way, the child will only play with the draft data and your parent view will only show the persistence data.
There's a good example here: https://developer.apple.com/tutorials/swiftui/working-with-ui-controls
Please have a look at all of the tutorials made by Apple, they are very short and powerful samples.
I will paste the code from the link above to here just in case the code is lost sometime in the future.
struct ProfileHost: View {
#Environment(\.editMode) var mode
#EnvironmentObject var userData: UserData
#State var draftProfile = Profile.default
var body: some View {
VStack(alignment: .leading, spacing: 20) {
HStack {
if self.mode?.wrappedValue == .active {
Button("Cancel") {
self.draftProfile = self.userData.profile
self.mode?.animation().wrappedValue = .inactive
}
}
Spacer()
EditButton()
}
if self.mode?.wrappedValue == .inactive {
ProfileSummary(profile: userData.profile)
} else {
ProfileEditor(profile: $draftProfile)
.onAppear {
self.draftProfile = self.userData.profile
}
.onDisappear {
self.userData.profile = self.draftProfile
}
}
}
.padding()
}
}
Related
I have a ForEach loop contained inside of a ZStack. The idea is to build a "Tinder-Like" interface. I have an issue where I might render 20-50 Cards which is a major strain on the system. It results in lagginess. If I render only 5, then it's much more responsive, however I do not know the proper structure to do this. I also need to be sure that I can "Re-loop" through the cards after the last one has been reached. This is what my struct looks like.
ZStack {
//MARK: - Card View
ForEach(googleApi.places) { place in
CardView(card: Card(isFavorite: false, place: place), homeModel: homeViewModel)
}
}.padding(.bottom)
I had a single view application wrapped in a NavigationView where I populate data fetched from an API. However, I needed to add a profile section with a TabView and sometimes when I run the application, right when the launch screen is displayed, I get the following error (about once every three times I run/ debug the app) and the app crashes.
AttributeGraph precondition failure: invalid value type for attribute: 56028 (expected PlatformItemList, saw BridgedTableViewState)
The view that causes the error:
TabView {
NavigationView {
MainView()
}
.tabItem { VStack {
Image(systemName: "list.dash")
Text("main")
}
}
NavigationView {
ProfileView()
}
.tabItem { VStack {
Image(systemName: "person.crop.circle")
Text("profile")
}
}
}
I removed the TabView and the profile view and the app runs without any exceptions consistently, which is why I thought it was an issue with the TabView. I searched for documentation on AttributeGraph, PlatformItemList and BridgedTableViewState but found none.
I had a related error: AttributeGraph precondition failure: invalid value type for attribute: 431640 (saw PreferenceKeys, expected ViewList).
This error did not arise in an app with almost identical functional and database code but using UIKit and manually-constructed tab view, list view etc. Only in the SwiftUI version with SwiftUI structs for these views.
Removing the Persistence class and persistence management seemed to resolve the issue (I use a direct SQLite coding for persistence instead of CoreData).
The Xcode error messages could be more helpful.
I had experienced same problem. It was caused by combination of AnyView and NSViewRepresentable wrapper for WKWebView. Getting rid of AnyView by rewriting helper function with #ViewBuilder fixed the problem.
Problem:
func viewFunc() -> AnyView {
switch self {
case .case1: return AnyView(ListOfWebViewWrappers())
default: return AnyView(WaitView())
}
}
Solution:
#ViewBuilder
func viewFunc() -> some View {
switch self {
case .case1: ListOfWebViewWrappers()
default: WaitView()
}
}
the #ViewBuilder allows SwiftUI to properly track each view's identity and properly manage resources. More information about view's identity: https://developer.apple.com/videos/play/wwdc2021/10022/
I spent a lot of time dealing with precondition failure crashes in an app with multiple tab views. Is there any chance that one or more of your specific views (ProfileView or MainView) is also using a NavigationView? If so, that's probably the problem. Xcode will happily let you add NavigationViews in each new view, but it's not actually the way SwiftUI is meant to work. Since each TabView is wrapped in a NavView, then you shouldn't need/use a NavView inside of those views.
(Also, probably just cosmetic, but in the code snippet above there seems to be an extra close curly brace before your second Nav view.)
I know that the component List has an UITableView behind. I also know I can change the background by changing the UITableView.appearance().backgroundColor.
What I want is to change this for a single View that has a List component, without affecting the rest of the application. Is this possible? If so, how?
Thanks!
This is what I know needs to be done to achieve this:
var body: some View {
List {
//Your content
}
.onAppear {
//The specific table view settings.
//UITableView.appearance().separatorColor = .black
}
.onDisappear {
//The general table view settings
//UITableView.appearance().separatorColor = nil
}
}
But it is problematic for several reasons.
The biggest reason is the fact you might have 2 table views in the same View and you want them to have different appearances.
The second issue is that not all cases will call onAppear (I don't remember specific cases but I remember having issues with TabViews)
I hope there's already a better solution waiting in the next version of SwiftUI.
I have a SwiftUI view:
struct CatView : View {
#State var eyesOpened: Bool = false
var body: some View {
Image(uiImage: eyesOpened ? #imageLiteral(resourceName: "OpenedEyesCat") : #imageLiteral(resourceName: "ClosedEyesCat"))
}
}
I'm trying to integrate it with in a regular UIViewController.
let hostingVC = UIHostingController<CatView>(rootView: cat)
addChild(hostingVC)
view.addSubview(hostingVC.view)
hostingVC.view.pinToBounds(of: view)
Now in the UIViewController if I try to set the eyesOpened property I get a
Thread 1: Fatal error: Accessing State<Bool> outside View.body
How are we supposed to make this work? Are SwiftUI views not supposed to work in this scenario?
#State is the wrong thing to use here. You'll need to use #ObservedObject.
#State: Used for when changes occur locally to your SwiftUI view - ie you change eyesOpened from a toggle or a button etc from within the SwiftUI view it self.
#ObservedObject: Binds your SwiftUI view to an external data source - ie an incoming notification or a change in your database, something external to your SwiftUI view.
I would highly recommend you watch the following WWDC video - Data Flow Through SwiftUI
There is something called the notification center that you could use. In short, it is a way that views can communicate between each other without actually modifying anything in each other.
How it works is view A sends a notification to a central hub from which view B hears said notification. When view B hears the notification it activates, and calls a function defined by the user.
For a more detailed explanation, you can refer to:
https://learnappmaking.com/notification-center-how-to-swift/
I want to update a SwiftUI List without any insert animation.
My List is getting its data from an #EnvironmentObject
I already tried to wrap the List itself and the PassthroughSubject.send() in a withAnimation(.empty) block but this does not help.
A very very dirty workaround is to call UIView.setAnimationsEnabled(false) (yes, UIKit has impact on SwiftUI), but there must be a SwiftUI-like way to set custom insert animations.
While the answer provided by DogCoffee works, it does so in an inefficient manner. Sometimes we do have to force the system to do what we want by being inefficient. In the case of implicit animations in SwiftUI, there is a better way to disable them.
Using the Transaction mechanism in SwiftUI, we can define an extension that can be applied to any view. This will disable animations for the view and any children.
For the list view example, this avoids replacing all the data in the list with a new, but identical copies.
extension View {
func animationsDisabled() -> some View {
return self.transaction { (tx: inout Transaction) in
tx.disablesAnimations = true
tx.animation = nil
}.animation(nil)
}
}
Try applying this extension to your list, or the parent containing view. You may have to experiment to find which view is ideal.
List {
// for each etc
}.animationsDisabled()
This works, just place .id(UUID()) at the end of your list
List {
// for each etc
}.id(UUID())
Sort of like reloadData for UIKit
on tvOS this works for me:
List {
...
}
.animation(.none)
.animate(nil)
you can find more info on https://developer.apple.com/tutorials/swiftui/animating-views-and-transitions