WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Update forces view to be dismissed (bug or error?) - UltimatePortfolio: Editing projects

Forums > Articles

@cgaaf  

I just completed the "Editing projects" article from the UltimatePortfolio series and I'm noticing a bug when I attempt to make any chnages in the EditProjectView. Each time I attempt to make any state change the view is immediately dismissed and I jump back to the ProjectsView. Example

This seems to specifically happen when the update() function is called. My update() function is identical to Paul's.

func update() {
        project.title = title
        project.detail = detail
        project.color = color
    }

So far I think it's related to the fact that the project property in the parent view (ProjectHeaderView) uses the @ObservedObject property wrapper. It seems that when the project property in the parent ProjectHeaderView updates, the EditProjectView within the NavigationLink gets dismissed. Is anyone else having this issue? It doesn't seem to happen in Paul's video. I've gone through and I don't see any obvious discrepencies between my code and Paul's.

What am I missing?

1      

@cgaaf  

So it looks like this was specifically a bug with the latest Xcode 12.2 Beta. It's working correctly on the Xcode 12.1 release

3      

@twostraws  Site AdminHWS+

Oh lovely. Please file some feedback with Apple – this might actually ship into production, and it's quite a breaking change!

2      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

I just sent an email to Paul about this. I thought it was an issue with the code from the latest lesson (adding/deleting).

It only occurs when there's two or more projects and works as intended with only one project.

1      

Thank you for the 'heads up'. I was going around in circles trying to get this to work (beta version), rolled back to 12.1 release and all works as it should!

1      

Was also stumped by this for a while in 12.2 beta 3, glad I came across this post! Thank you, will also file some feedback!

1      

This was driving me nuts as well and I was thinking I'd introduced a bug somewhere random! Thanks for finding the issue.

Xcode 12.2 beta 4 (RC) seems to have resolved the issue - good job, otherwise we'd be pushing back on that version :-)

1      

Well, I'm late to the party, but I got the same bug in Xcode 12.2 (non-beta)

Did anyone figure out something that really solves that bug/problem?

1      

It seems to be fixed in Xcode 12.3 beta (12C5020f)

1      

Not working here on latest beta.

1      

Same bug here with Xcode 12.2 on Catalina 10.15.7. Editing items is fine but making any edit to a Project causes the instance of EditProjectView to be dismissed as soon as the handler is called.

1      

This bug still exists in Xcode 12.3

1      

Bug is still an issue with Xcode 12.3(12C33)

2      

I am still able to recreate the bug consistently with the latest Xcode, as well. I'm working around the bug with the following line. It solves the issue enough for me, but negates the use of the onChange() extension.

TextField("Project name", text: $title, onEditingChanged: { if !$0 { update() } })

The following also works, but requires return to be pressed.

TextField("Project name", text: $title, onCommit: update)

2      

Has anyone a fix for this? It's still there with Xcode 12.4.

1      

It appears to be fixed once again in 12.5 beta (12E5220o).

1      

great

1      

thanks gor sharing great information if you want to read more stuff like that visit: new post from sportsshow

1      

I don't think this is solved, or at least not in all situations. I believe I'm hitting this same circumstance.

XCode 13.2.1 MacOS 12.2.1 iPhone 13 simulator iOS 15.something

I'm genning up an object in the root view and passing it into the environment using @environmentObject(...), navigating via a NavigationView in the root view, updating things along the way (in this case the object is a data collector for account signup information across several screens). I get a few screens in, and whammo, any udpates to the @EnvironmentObject private var signupData: SignupData object causes a dismiss. It's not consistent (meaning which screen will do it), but whatever screen it happens on, happens 100% of the time.

I don't think there's anything overly fancy about my data collector object, and even on a stripped-to-the-bone test View, it still happens (pasted below)

The data collector object

import Foundation

enum AccountType: Int {
    case none
    case journalOnly
    case singleIncident
    case preventive
}

class SignupInfo : ObservableObject {
    // seeding sample data for less typing during dev & testing
    @Published var email: String = "bob@bobberson.com"
    @Published var password: String = "skatanna99!"
    @Published var password2: String = "skatanna99!"

    @Published var firstname: String = "bob"
    @Published var lastname: String = "bobberson"

    @Published var address1: String = "123 Some Town Road"
    @Published var address2: String = ""
    @Published var city: String = "Newark"
    @Published var region: String = "DE"
    @Published var postalCode: String = "19701"

    @Published var phone: String = ""

    @Published var ccnumber: String = ""
    @Published var ccname: String = ""

    @Published var accountType: AccountType = .journalOnly
}

(not real info of course)

The view that's crashing out or dismissing on data change. I wondered if it was a threading thing, so I have with and without a dispatch. No change to the behavior. I think it's related to the depth into the navigationLink stack.

import SwiftUI

struct SignupServicesView: View {

    //@StateObject private var viewModel:ViewModel = ViewModel()
    @EnvironmentObject private var signupInfo: SignupInfo
    //@EnvironmentObject private var opData: OpData

    var body: some View {
        Text("account type is set to \(signupInfo.accountType.rawValue)")
            .onTapGesture {
                signupInfo.accountType = .preventive
            }
            .onLongPressGesture {
                DispatchQueue.main.async {
                    signupInfo.accountType = .preventive
                }
            }
    }

}

frustrating!

   

And moving from @ObservedObject (of some far-off object) or @EnvironmentObject to just parking the data in my viewModel works as you would expect. Just posting this for the sake of completeness.

import SwiftUI

struct SignupServicesView: View {

    @StateObject private var viewModel:ViewModel = ViewModel()
    //@EnvironmentObject private var opData: OpData

    var body: some View {
        Text("account type is set to \(viewModel.accountType.rawValue)")
            .onTapGesture {
                viewModel.accountType = .preventive
            }
            .onLongPressGesture {
                DispatchQueue.main.async {
                    viewModel.accountType = .preventive
                }
            }
            .navigationBarHidden(true)
    }

}

and the view model object...

import Foundation

extension SignupServicesView {
    class ViewModel : ObservableObject {
        @Published var accountType: AccountType = .journalOnly
    }
}

   

It's not the enum data type. I removed it. Even doing basic stuff like changing a string is a problem. Here's my ongoing testing scenario...

Interestingly, when I move things into the viewModel, it all works normal, as expected. No surprise. But I still want a common place to drop the data, without creating CoreData or UserDefaults entries.

When I move the underlying data collector object into a global context and attach it to the view model, it works if I hop the properties through the view model (view updates view model, view model updates global object).

But if I just expose the underlying data connector directly it still dismisses the View in this crazy weird way.

Hang the global object off the app's main view (for testing only)

public static var signupInfo = SignupInfo()

Then in the viewModel...

import Foundation

extension SignupServicesView {
    class ViewModel : ObservableObject {
        // works great
        @Published var firstname = theApp.signupInfo.firstname
    }
}

And the View:

import SwiftUI

struct SignupServicesView: View {

    @StateObject private var viewModel:ViewModel = ViewModel()

    var body: some View {
        Text("account type is set to \(viewModel.firstname)")
            .onTapGesture {
                viewModel.firstname = "joe"
            }
            .onLongPressGesture {
                DispatchQueue.main.async {
                    viewModel.firstname = "joe"
                }
            }
            .navigationBarHidden(true)
    }
}

That all works fantastic.

Exposing the data directly doesn't work. The viewModel:

import Foundation

extension SignupServicesView {
    class ViewModel : ObservableObject {
        @Published var signupInfo = theApp.signupInfo
    }
}

The View:

import SwiftUI

struct SignupServicesView: View {

    @StateObject private var viewModel:ViewModel = ViewModel()

    var body: some View {
        Text("account type is set to \(viewModel.signupInfo.firstname)")
            .onTapGesture {
                viewModel.signupInfo.firstname = "joe"
            }
            .onLongPressGesture {
                DispatchQueue.main.async {
                    viewModel.signupInfo.firstname = "joe"
                }
            }
            .navigationBarHidden(true)
    }
}

So, it really feels like talking to my SignupInfo object directly is a problem for (some of) my Views when they are deep-ish into the Navigation stack, no matter how they become aware of it. But I'll be a pickled monkey if I can figure out why.

   

Solved redux: for reasons that make no sense to me on initial blush, because I'm not updating any of the values used on the previous screen, it seems it was forcing a redraw on the previous screen, which then, again for reasons that simply don't make much sense, pops the current view in favor of foregrounding and redrawing the previous screen.

I fully understand the HOW of it, but the WHY of it is... ahem... pardon my french... pretty f$#cking stupid. I don't often scratch my head at the way Apple does things, but when I do, it's because they've gone to new levels of "what the heck were thinking, boy?"

(sigh)

Anyway, I just hid the data collector object behind ViewModels for each screen, and it's exposed at the root View's level as a static global. Cheaty. Hackey. Effective. Done.

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.