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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSURLSessionTask priority #2174

Open
ntnmrndn opened this issue Jul 20, 2021 · 1 comment · May be fixed by #2330
Open

NSURLSessionTask priority #2174

ntnmrndn opened this issue Jul 20, 2021 · 1 comment · May be fixed by #2330

Comments

@ntnmrndn
Copy link

Hello,

I was wondering wether Moya would consider adding support for prioritisation through NSURLSessionTask .priority property.

Do you have thoughts on the subject ?

@ntnmrndn ntnmrndn linked a pull request Nov 8, 2023 that will close this issue
@ljluestc
Copy link

import Moya
import Foundation

class PrioritizedMoyaProvider<Target>: MoyaProvider<Target> where Target: TargetType {
    
    override func request(_ target: Target, callbackQueue: DispatchQueue? = .none, progress: ProgressBlock? = .none, completion: @escaping Completion) -> Cancellable {
        let requestCompletion: Completion = { result in
            // Call the original completion handler with the result
            completion(result)
        }
        
        // Create a URLRequest using the target
        let urlRequest = try! target.urlRequest()
        
        // Create a URLSessionTask from the URLRequest
        let task = session.dataTask(with: urlRequest) { data, response, error in
            // Process the URLSessionTask response
            let result = Result<Response, MoyaError>.init(catching: {
                // Handle the response and error to create a Moya Response or Moya Error
                // Note: This logic should be adapted to fit your specific needs
                // For demonstration purposes, we'll just create a simple response
                let response = Response(statusCode: 200, data: data ?? Data())
                return response
            })
            
            // Call the request completion handler with the result
            requestCompletion(result)
        }
        
        // Set the task priority based on your logic
        // For demonstration purposes, we'll set it to high priority for all requests
        task.priority = URLSessionTask.highPriority
        
        // Start the URLSessionTask
        task.resume()
        
        // Return a Cancellable object representing the URLSessionTask
        return AnyCancellable(task)
    }
}

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 a pull request may close this issue.

2 participants