Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-11373] Add support for deferring view load span end #230

Merged
merged 1 commit into from Dec 27, 2023

Conversation

kstenerud
Copy link
Contributor

@kstenerud kstenerud commented Dec 27, 2023

Goal

Add defer APIs to allow the user to defer the end of the overarching view load span based on a condition:

    /**
     * Defer ending the overarching view load span until the supplied function returns true during a body build cycle.
     * The view load span will not be ended until ALL deferred-end conditions are true.
     */
    func bugsnagDeferEndUntil(deferUntil: @escaping ()->(Bool)) -> some View

    /**
     * Defer ending the overarching view load span until this view disappears from the view hierarchy.
     * The view load span will not be ended until ALL deferred-end conditions are true.
     */
    func bugsnagDeferEndUntilViewDisappears() -> some View

Design

Views can now have their overarching view load span end deferred like so:

struct SomeView: View {
    @State var data: Data?
    @State var deferringViewLoadSpan = true

    var body: some View {

        // Fake a background data load.
        // On iOS 15+ you'd use a .task()
        defer {
            DispatchQueue.global().async {
                data = Data()
            }
        }

        return VStack {
            if data == nil {
                // .bugsnagDeferEndUntilViewDisappears() will hold the current
                // view load span open until this Text view disappears.
                //
                // When this Text view disappears, its defer is resolved.
                // That would in theory leave the view load span free to end,
                // but there are still other defers to resolve!
                Text("SwiftUI is loading")
                .bugsnagTraced("loading")
                .bugsnagDeferEndUntilViewDisappears()
            } else {
                Text("Hello from SwiftUI 馃檭")
                .bugsnagTraced("loaded")

                if deferringViewLoadSpan {
                    // Defer the view load span end until this button disappears.
                    Button("Stop deferring the view load span") {
                        deferringViewLoadSpan = false
                    }
                    .bugsnagDeferEndUntilViewDisappears()
                }
            }
        }
        .bugsnagDeferEndUntil {
            // Defer the view load span end until this function returns a true value.
            //
            // This is technically redundant since the above button is deferring the
            // view load span based on the same @State value (deferringViewLoadSpan).
            return !deferringViewLoadSpan
        }
    }
}

Testing

Added e2e tests

Copy link

BugsnagPerformance.framework binary size did not change - 489,664 bytes

    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [ = ]       0  [ = ]       0    TOTAL

Generated by 馃毇 Danger

@kstenerud kstenerud merged commit 28519c3 into next Dec 27, 2023
21 checks passed
@kstenerud kstenerud deleted the PLAT-11373-defer-view-span-end branch December 27, 2023 11:35
@robert-smartbear robert-smartbear mentioned this pull request Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants