kean / Pulse

Network logger for Apple platforms
https://pulselogger.com
MIT License
6.32k stars 302 forks source link

Async await support #143

Closed Faktorealchik closed 1 year ago

Faktorealchik commented 1 year ago

I tried test project with async await, and I think it's not working

Could you please give me a hint how it might be used with async/await ? Do you plan to add support for it?

changed test code:

// The MIT License (MIT)
//
// Copyright (c) 2020–2023 Alexander Grebenyuk (github.com/kean).

import SwiftUI
import Pulse
import PulseUI

@main
struct Pulse_Demo_iOSApp: App {
    var body: some Scene {
        let _ = testProxy()
        WindowGroup {
            NavigationView {
                ConsoleView()
            }
        }
    }
}

private func testProxy() {
    URLSessionProxyDelegate.enableAutomaticRegistration()

    let session = URLSession(configuration: .default, delegate: MockSessionDelegate(), delegateQueue: nil)
    let request = URLRequest(url: URL(string: "https://catfact.ninja/fact")!)

    Task {
        do {
            let response = try await session.data(for: request)
            print(response)
        } catch {
            print(error)
        }
    }
}

private final class MockSessionDelegate: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDownloadDelegate {
    var completion: ((URLSessionTask, Error?) -> Void)?

    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        completion?(task, error)
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("here")
    }
}

screenshot of the app after the code is completed:

Screenshot 2023-01-21 at 10 44 11

Also I've tried my own app, the new request are showing in console view, but they are stale in 'pending' status

Faktorealchik commented 1 year ago

https://github.com/kean/Pulse/issues/113 duplicate