From 285b4fd1b2eb11129c583514267a4060335094d1 Mon Sep 17 00:00:00 2001 From: Chris Dillard Date: Thu, 16 Jan 2025 16:09:42 -0700 Subject: [PATCH] Add example command to NRTestApp for async data task --- .../NRTestApp/ViewModels/UtilViewModel.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift b/Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift index 484ec402..2911ddab 100644 --- a/Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift +++ b/Test Harness/NRTestApp/NRTestApp/ViewModels/UtilViewModel.swift @@ -43,6 +43,12 @@ class UtilViewModel { options.append(UtilOption(title: "Notice Network Request", handler: { [self] in noticeNWRequest()})) options.append(UtilOption(title: "Notice Network Failure", handler: { [self] in noticeFailedNWRequest()})) options.append(UtilOption(title: "URLSession dataTask", handler: { [self] in doDataTask()})) + options.append(UtilOption(title: "Async URLSession dataTask", handler: { [self] in + Task { + try await doAsyncDataTask() + } + })) + options.append(UtilOption(title: "Shut down New Relic Agent", handler: { [self] in shutDown()})) } @@ -157,6 +163,18 @@ class UtilViewModel { dataTask.resume() } + // Async + func doAsyncDataTask() async throws { + let urlSession = URLSession(configuration: URLSession.shared.configuration, delegate: taskProcessor, delegateQueue: nil) + + guard let url = URL(string: "https://www.google.com") else { return } + + let request = URLRequest(url: url) + let (data, _) = try await urlSession.data(for: request) + + print("Data: \(data)") + } + func shutDown() { NewRelic.shutdown() }