NSUrlSessionDataDelegate : How to identify endpoint name of REST Api

amolasamolas Member ✭✭

Hi,

We have implemented NSUrlSessionDataDelegate and getting response of GET and POST requests in that delegate.

We need to start 3 NSUrlRequests simultaneously. In DidCompleteWithError callback how can we identify that the response corresponds to a particular request of those 3 requests made. We need to identify this because we have written a common class as network request manager.

We have sent header parameters for inside requests like request.Headers.SetValueForKey(new NSString("OurEndpointName"), new NSString("API"));

In DidCompleteWithError callback we try to retrieve the header parameters sent .But we are not getting those parameters.We tried following 2 ways to read header back

1.NSString strHeader = (NSString)task.CurrentRequest.Headers.ValueForKey(new NSString("API"));
2.NSString strHeader = (NSString)task.OriginalRequest.Headers.ValueForKey(new NSString("API"));

Both of these ways return null.

We have thought of url, absolutepath, relative path parameters in response, but some of the requests have common url structure and they differ only in request parameters.

Thanks & best regards,

Amol Sarmalkar

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    When you create a data task, the system will allocate a unique identifier to this task you can differ the tasks through this identifier:

    NSUrlSession session = NSUrlSession.FromConfiguration(NSUrlSessionConfiguration.DefaultSessionConfiguration, new MySessionDelegate() as INSUrlSessionDelegate, new NSOperationQueue());
    NSUrlSessionDataTask task = session.CreateDataTask(new NSUrl("Your Request URL"));
    Debug.WriteLine(task.TaskIdentifier);
    task.Resume();
    
    public class MySessionDelegate : NSUrlSessionDataDelegate
    {
        public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
        {
            Debug.WriteLine(dataTask.TaskIdentifier);
        }
    }
    

    Alternative, you can manually set a description to the tasks:

    task.TaskDescription = "Task One";
    Then retrieve that value in the delegate.

Answers

  • amolasamolas Member ✭✭

    Basically we need access to functions of class NSUrlRequest and NSMutableUrlRequest written below

    [Export ("valueForHTTPHeaderField:")]
    [BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
    internal virtual string Header (string field);

    [Export ("setValue:forHTTPHeaderField:")]
    [BindingImpl (BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
    internal virtual void _SetValue (string value, string field);

    Because they are internal we are not able to access them.

    If we can get access to these functions then our problem would get solved. We tried above methods in native app and native app is able to access header fields added while creating session tasks.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    When you create a data task, the system will allocate a unique identifier to this task you can differ the tasks through this identifier:

    NSUrlSession session = NSUrlSession.FromConfiguration(NSUrlSessionConfiguration.DefaultSessionConfiguration, new MySessionDelegate() as INSUrlSessionDelegate, new NSOperationQueue());
    NSUrlSessionDataTask task = session.CreateDataTask(new NSUrl("Your Request URL"));
    Debug.WriteLine(task.TaskIdentifier);
    task.Resume();
    
    public class MySessionDelegate : NSUrlSessionDataDelegate
    {
        public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
        {
            Debug.WriteLine(dataTask.TaskIdentifier);
        }
    }
    

    Alternative, you can manually set a description to the tasks:

    task.TaskDescription = "Task One";
    Then retrieve that value in the delegate.

Sign In or Register to comment.