0

I'm currently using a custom NavigationStack to control my navigations, as proposed in this article.

I am able to run Build, Simulate, Test, and Analyze successfully, but when running Archive, Xcode throws an error: Segmentation fault: 11, right before completion.

I've pinpointed the code causing this error to a spot within the advance(_:) method (in NavigationStack.swift) through trial and error.

    func advance<V: View & Navigatable>(_ view: V) {
        let item = NavigationItem(view)
        viewStack.append( currentView )
        withAnimation {
            self.navigate = .forward
        }
        //>>>>>>>> HERE'S THE PROBLEM <<<<<<<<<
        // ↓this↓ causes the Archive to fail.
        currentView = item
    }

My Environment: macOS Catalina - Version 10.15.2, Xcode - Version 11.3 (11C29). (However, I had this issue before updating from macOSCatalina10.15.1 and Xcode11.2)

If it helps, my other source codes are as follows:

SceneDelegate.swift:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
    let contentView = ContentView()//.environmentObject(UserData())
...
}

ContentView.swift:

struct ContentView: View {
    //@EnvironmentObject var userData: UserData   
    var body: some View {
        NavigationHost()
            //.environmentObject(NavigationStack( --Commented out in Edit
            //    NavigationItem(view: AnyView( HomeView() ) --Commented out in Edit
            .environmentObject(NavigationStack( HomeView() ))
                    .transition(.move(edge: .trailing))
            //.environmentObject(userData)
    }
}

NavigationHost.swift:

struct NavigationHost: View{
    @EnvironmentObject var navigation: NavigationStack
    //@EnvironmentObject var userData: UserData

    var body: some View {
            ZStack {
                   if self.navigation.navigate == .root {
                    self.navigation.currentView.view
                    //.environmentObject(self.userData)
                    .environmentObject(self.navigation)
                }
                else if self.navigation.navigate == .backward {
                    self.navigation.currentView.view
                    //.environmentObject(self.userData)
                    .environmentObject(self.navigation)
                        .transition(.move(edge: .leading))
                }
                if self.navigation.navigate == .forward {
                    self.navigation.currentView.view
                    //.environmentObject(self.userData)
                    .environmentObject(self.navigation)
                        .transition(.move(edge: .trailing))
                } 
                   ...
          }
    }
}

NavigationStack.swift:

final class NavigationStack: ObservableObject {
    enum Direction {
        case root
        case forward
        case backward
    }

    @Published var viewStack: [NavigationItem] = []
    @Published var currentView: NavigationItem
    @Published var navigate: Direction = .root
    @Published var turnOnCloseButton: Bool = false
    @Published var turnOnBackButton: Bool = false

    init<V: View & Navigatable>(_ view: V, description: String? = nil) {
        let currentView = NavigationItem(view, description: description)
        print("Navigation Stack Initialized")
        self.currentView = currentView
    }


    func back(turnOffBack: Bool = true, turnOffClose: Bool = true) {
        if turnOffBack { turnOnBackButton = false }
        if turnOffClose { turnOnCloseButton = false }
        if viewStack.count == 0 {
            return
        }
        let last = viewStack.count - 1
        currentView = viewStack[last]
        withAnimation {
            self.navigate = .backward
        }
        viewStack.remove(at: last)
    }

    func advance<V: View & Navigatable>(_ view: V) {
        let item = NavigationItem(view)
        viewStack.append( currentView )
        withAnimation {
            self.navigate = .forward
        }
        //>>>>>>>> HERE'S THE PROBLEM <<<<<<<<<
        // ↓this↓ causes the Archive to fail.
        currentView = item
    }

    func home() {
        currentView = NavigationItem( HomeeView() )
        viewStack.removeAll()
        withAnimation {
            self.navigate = .root
        }
    }

    func bringToFront<V: View & Navigatable>(_ view: V) {
        let item = NavigationItem(view)
        viewStack = viewStack.filter { $0 != item }
        //advance(view)
    }

}

struct NavigationItem: Equatable{
    var view: AnyView
    let id: String = UUID().uuidString
    var description: String

    init< V: View & Navigatable >(_ view: V, description: String? = nil) {
        self.view = AnyView(view)
        if description==nil {
            self.description = V.getDescription(view)()//"\(type(of: view))"
        } else {
            self.description = description!
        }
    }

    static func == (lhs: NavigationItem, rhs: NavigationItem) -> Bool {
        return lhs.description == rhs.description
    }
}

protocol Navigatable {
    var description: String { get }
}

extension Navigatable {
    func getDescription() -> String {
        return "\(type(of: self))"
    }
}

I've also asked a question regarding NavigationStack previously, but a different issue.

I've sent a bug report via Feedback Assistant 2 weeks ago, but haven't gotten anywhere yet.

I'm in a rush to deploy a TestFlight, so any help is much appreciated!

Edit: Here's a part of the error message from the Archive failure:

...
1.  While running pass #41789 SILFunctionTransform "GenericSpecializer" on SILFunction "@$s8okan_app15NavigationStackC7advanceyyx7SwiftUI4ViewRzAA11NavigatableRzlFAA04NextH0V_Tg5".
 for 'advance(_:)' (at /Users/myname/Downloads/my-project/my-project/Navigations/NavigationStack.swift:48:2)
0  swift                    0x0000000112c88a63 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000112c88236 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff6627942d _sigtramp + 29
3  libsystem_platform.dylib 0x0000000800000001 _sigtramp + 2581097457
4  swift                    0x000000010f0cd08c swift::ReabstractionInfo::prepareAndCheck(swift::ApplySite, swift::SILFunction*, swift::SubstitutionMap, swift::OptRemark::Emitter*) + 572
5  swift                    0x000000010f11fc5a swift::ReabstractionInfo::ReabstractionInfo(swift::ApplySite, swift::SILFunction*, swift::SubstitutionMap, swift::IsSerialized_t, bool, swift::OptRemark::Emitter*) + 122
6  swift                    0x000000010f129cb5 swift::trySpecializeApplyOfGeneric(swift::SILOptFunctionBuilder&, swift::ApplySite, llvm::SmallSetVector<swift::SILInstruction*, 8u>&, llvm::SmallVectorImpl<swift::SILFunction*>&, swift::OptRemark::Emitter&) + 1653
7  swift                    0x000000010f0027b1 (anonymous namespace)::GenericSpecializer::run() + 2673
8  swift                    0x000000010ef7bfbe swift::SILPassManager::execute() + 4606
9  swift                    0x000000010ebda9eb swift::CompilerInstance::performSILProcessing(swift::SILModule*, swift::UnifiedStatsReporter*) + 6379
10 swift                    0x000000010e8d2f45 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 33925
11 swift                    0x000000010e8c72b4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
12 swift                    0x000000010e8547b3 main + 1219
13 libdyld.dylib            0x00007fff660807fd start + 1
14 libdyld.dylib            0x00000000000000d2 start + 2583165142
error: Segmentation fault: 11 (in target 'my-project' from project 'my-project')
q8yas
  • 897
  • 8
  • 10
Miss Swiss
  • 89
  • 1
  • 9
  • 1
    Did you try to place this `currentView = item` line _before_ `withAnimation` block? – Asperi Dec 12 '19 at 14:59
  • Are you sure you really need to implement your own custom navigation? It's possible to customize the existing navigation bar: https://stackoverflow.com/a/58427754/467209 – arsenius Dec 13 '19 at 00:54
  • @Asperi I haven't. I'll try that. – Miss Swiss Dec 13 '19 at 00:58
  • @arsenius frankly I'm not completely confident I do. My app has some complicated navigations, and since `NavigationView` was a black box for me, I didn't know how realize them with `NavigationView`. Since I have complete control over my navigations with a custom `NavigationStack`, and had no problem with bugs so far, I've just used it ever since. However, if this doesn't seem to be able to resolve, I will have to drop this idea. – Miss Swiss Dec 13 '19 at 01:10
  • @Asperi Update: changing the order to having the `currentView = item` line before `withAnimation` block didn't work. Thanks for the idea tho:) – Miss Swiss Dec 13 '19 at 01:25

1 Answers1

2

Either you miss some codes, or there are some errors:

The NavigationStack has to be called as the following:

var body: some View {
    NavigationHost()
        .environmentObject(NavigationStack(
            HomeView() ))

        //.environmentObject(userData)
}
E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • Oops, sorry. This is due to my copying and pasting some of the source code from a different question. My latest code is written as `.environmentObject(NavigationStack( HomeView() ))`, as you've pointed out. I'll change it, thanks:) – Miss Swiss Dec 13 '19 at 00:54
  • Just to note, my code successfully runs Build, Simulate, and Analyze, but only fails when attempting to Archive. – Miss Swiss Dec 13 '19 at 01:15
  • Some archive problem are there are some duplicate variables. Or enum raw value is not correct – E.Coms Dec 13 '19 at 01:31
  • There is some hidden problem. – E.Coms Dec 13 '19 at 01:33
  • >duplicate variables. Or enum raw value is not correct. I added to my question a part of the error message from the archive. Is there a way to infer if your proposed problems may be the cause of the error? – Miss Swiss Dec 13 '19 at 01:54
  • ANY VIEW you will use must comform to `View & Navigatable` at the same time. If any view misses one protocol, you may have some this information. – E.Coms Dec 13 '19 at 03:40