Skip to content

Commit 285b4fd

Browse files
Add example command to NRTestApp for async data task
1 parent 1efe786 commit 285b4fd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class UtilViewModel {
4343
options.append(UtilOption(title: "Notice Network Request", handler: { [self] in noticeNWRequest()}))
4444
options.append(UtilOption(title: "Notice Network Failure", handler: { [self] in noticeFailedNWRequest()}))
4545
options.append(UtilOption(title: "URLSession dataTask", handler: { [self] in doDataTask()}))
46+
options.append(UtilOption(title: "Async URLSession dataTask", handler: { [self] in
47+
Task {
48+
try await doAsyncDataTask()
49+
}
50+
}))
51+
4652
options.append(UtilOption(title: "Shut down New Relic Agent", handler: { [self] in shutDown()}))
4753
}
4854

@@ -157,6 +163,18 @@ class UtilViewModel {
157163
dataTask.resume()
158164
}
159165

166+
// Async
167+
func doAsyncDataTask() async throws {
168+
let urlSession = URLSession(configuration: URLSession.shared.configuration, delegate: taskProcessor, delegateQueue: nil)
169+
170+
guard let url = URL(string: "https://www.google.com") else { return }
171+
172+
let request = URLRequest(url: url)
173+
let (data, _) = try await urlSession.data(for: request)
174+
175+
print("Data: \(data)")
176+
}
177+
160178
func shutDown() {
161179
NewRelic.shutdown()
162180
}

0 commit comments

Comments
 (0)