If your app uses SwiftUI, view load measurements can be easily captured on your BugSnag dashboard using our SwiftUI package.
Add the BugsnagPerformanceSwift
pod to your Podfile
:
pod 'BugsnagPerformanceSwift'
Run pod install
after updating your Podfile
.
Open your Xcode project and select File → Add Packages…
Search for https://github.com/bugsnag/bugsnag-cocoa-performance
as the package URL
Set Dependency Rule exact version
to v1.15.0
, then click Add Package.
When prompted on the next screen, select the BugsnagPerformanceSwift
package product to add it to your project.
Under the General tab, click the Frameworks, Libraries and Embedded Content section’s +
button and select BugsnagPerformanceSwift.framework
(from BugsnagPerformanceSwift
).
Create a BugsnagTracedView
to wrap each view that you want to measure:
import SwiftUI
import BugsnagPerformanceSwift
struct ContentView: View {
var body: some View {
BugsnagTracedView("MyView"){
VStack {
// ...
}
}
}
}
If no name is provided, the BugSnag SDK will use the view’s description.
Alternatively, you can call bugsnagTraced
on an existing view to achieve the same effect:
import SwiftUI
import BugsnagPerformanceSwift
struct ContentView: View {
var body: some View {
VStack {
// ...
}
.bugsnagTraced("MyView")
}
}
The top-level instrumented view’s duration metrics will be aggregated in the “Screen loads” tab in the BugSnag dashboard. The SDK records the time from when its processing starts until the end of the work on the current main queue, which will mean the entire visible SwiftUI view hierarchy has loaded.
You can also instrument nested view components. These will appear underneath the top-level span in the waterfall diagram of the span instance view on your dashboard. They are represented as a zero-length span at the moment their body
property runs.
From a user’s perspective, the view is not loaded until it is ready for use. For this reason we provide APIs to allow you to defer the end of a view’s span – either until a condition is met or particular nested view components disappear (for example a loading spinner).
bugsnagDeferEndUntil
takes a function that provides the conditions for closing the span:
import SwiftUI
import BugsnagPerformanceSwift
struct ContentView: View {
var body: some View {
VStack {
// ...
}
.bugsnagDeferEndUntil {
return !isLoading
}
}
}
bugsnagDeferEndUntilViewDisappears
is added to nested components of a view to keep the span open until they disappear. This is intended to be placed on loading messages/spinners, for example:
import SwiftUI
import BugsnagPerformanceSwift
struct LoadingView: View {
var body: some View {
Text("Loading...")
.bugsnagDeferEndUntilViewDisappears()
}
}
struct ContentView: View {
var body: some View {
VStack {
if data == nil {
LoadingView()
} else {
Text("View loaded")
}
// ...
}
.bugsnagTraced("MyView")
}
}