How to Find which response belongs to which request in Async api calling using URLSession












0















I'm using URLSession to get the response from the API's in my iPhone app. In my case, I'm hitting around 10 requests of a same api at a time using Asynchronous API calling and I'm getting the response for all the requests. Now from those responses how can I find which response belongs to which request? Any help appreciated.










share|improve this question

























  • Would you like to include the code you have tried so far?

    – Kamran
    Nov 28 '18 at 13:25
















0















I'm using URLSession to get the response from the API's in my iPhone app. In my case, I'm hitting around 10 requests of a same api at a time using Asynchronous API calling and I'm getting the response for all the requests. Now from those responses how can I find which response belongs to which request? Any help appreciated.










share|improve this question

























  • Would you like to include the code you have tried so far?

    – Kamran
    Nov 28 '18 at 13:25














0












0








0


1






I'm using URLSession to get the response from the API's in my iPhone app. In my case, I'm hitting around 10 requests of a same api at a time using Asynchronous API calling and I'm getting the response for all the requests. Now from those responses how can I find which response belongs to which request? Any help appreciated.










share|improve this question
















I'm using URLSession to get the response from the API's in my iPhone app. In my case, I'm hitting around 10 requests of a same api at a time using Asynchronous API calling and I'm getting the response for all the requests. Now from those responses how can I find which response belongs to which request? Any help appreciated.







swift asynchronous request response nsurlsession






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 13:27







Ramakrishna

















asked Nov 28 '18 at 12:24









RamakrishnaRamakrishna

438418




438418













  • Would you like to include the code you have tried so far?

    – Kamran
    Nov 28 '18 at 13:25



















  • Would you like to include the code you have tried so far?

    – Kamran
    Nov 28 '18 at 13:25

















Would you like to include the code you have tried so far?

– Kamran
Nov 28 '18 at 13:25





Would you like to include the code you have tried so far?

– Kamran
Nov 28 '18 at 13:25












1 Answer
1






active

oldest

votes


















1














You have many ways to achieve this.



1 - Basically, if you call your client function with an identifier, you will be able to retrieve it in your completion block:



func call(with identifier: String, at url: URL) {
URLSession.shared.dataTask(url: url) { (_, _, _) in
print(identifier)
}.resume()
}


2 - You can also use the taskIdentifier of an URLSessionDataTask. But to do this, you will need to use the delegate of your custom URLSession:



self.session = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: nil)


then you will not use a completion block but the delegate function instead:



func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(dataTask.taskIdentifier)
}


(of course you need to know which task identifier has been set for which URLSessionDataTask)



3 - If you need to access your identifier from your completion block, you can write a function which will happened it in the list of the parameter of the default completion block:



func dataTask(session: URLSession,
url: URL,
identifier: String,
completionBlock: @escaping (String, Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return session.dataTask(with: url) { (data, response, error) in
completionBlock(identifier, data, response, error)
}
}


4 - If you need to have a custom identifier in a URLSessionDataTask object, you can add it using extension and associated object:



extension URLSessionDataTask {
var identifier: String? {
get {
let identifier = objc_getAssociatedObject(self, &kIdentiferId)
if let id = identifier as? String {
return id
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &kIdentiferId, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

private var kIdentiferId: Int8 = 100


Then you can use it like this:



let task = session.dataTask(url: url)
task.identifier = "hello"





share|improve this answer
























  • Thanks. This solution worked for me.

    – Ramakrishna
    Dec 6 '18 at 9:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53519430%2fhow-to-find-which-response-belongs-to-which-request-in-async-api-calling-using-u%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have many ways to achieve this.



1 - Basically, if you call your client function with an identifier, you will be able to retrieve it in your completion block:



func call(with identifier: String, at url: URL) {
URLSession.shared.dataTask(url: url) { (_, _, _) in
print(identifier)
}.resume()
}


2 - You can also use the taskIdentifier of an URLSessionDataTask. But to do this, you will need to use the delegate of your custom URLSession:



self.session = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: nil)


then you will not use a completion block but the delegate function instead:



func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(dataTask.taskIdentifier)
}


(of course you need to know which task identifier has been set for which URLSessionDataTask)



3 - If you need to access your identifier from your completion block, you can write a function which will happened it in the list of the parameter of the default completion block:



func dataTask(session: URLSession,
url: URL,
identifier: String,
completionBlock: @escaping (String, Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return session.dataTask(with: url) { (data, response, error) in
completionBlock(identifier, data, response, error)
}
}


4 - If you need to have a custom identifier in a URLSessionDataTask object, you can add it using extension and associated object:



extension URLSessionDataTask {
var identifier: String? {
get {
let identifier = objc_getAssociatedObject(self, &kIdentiferId)
if let id = identifier as? String {
return id
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &kIdentiferId, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

private var kIdentiferId: Int8 = 100


Then you can use it like this:



let task = session.dataTask(url: url)
task.identifier = "hello"





share|improve this answer
























  • Thanks. This solution worked for me.

    – Ramakrishna
    Dec 6 '18 at 9:01
















1














You have many ways to achieve this.



1 - Basically, if you call your client function with an identifier, you will be able to retrieve it in your completion block:



func call(with identifier: String, at url: URL) {
URLSession.shared.dataTask(url: url) { (_, _, _) in
print(identifier)
}.resume()
}


2 - You can also use the taskIdentifier of an URLSessionDataTask. But to do this, you will need to use the delegate of your custom URLSession:



self.session = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: nil)


then you will not use a completion block but the delegate function instead:



func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(dataTask.taskIdentifier)
}


(of course you need to know which task identifier has been set for which URLSessionDataTask)



3 - If you need to access your identifier from your completion block, you can write a function which will happened it in the list of the parameter of the default completion block:



func dataTask(session: URLSession,
url: URL,
identifier: String,
completionBlock: @escaping (String, Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return session.dataTask(with: url) { (data, response, error) in
completionBlock(identifier, data, response, error)
}
}


4 - If you need to have a custom identifier in a URLSessionDataTask object, you can add it using extension and associated object:



extension URLSessionDataTask {
var identifier: String? {
get {
let identifier = objc_getAssociatedObject(self, &kIdentiferId)
if let id = identifier as? String {
return id
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &kIdentiferId, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

private var kIdentiferId: Int8 = 100


Then you can use it like this:



let task = session.dataTask(url: url)
task.identifier = "hello"





share|improve this answer
























  • Thanks. This solution worked for me.

    – Ramakrishna
    Dec 6 '18 at 9:01














1












1








1







You have many ways to achieve this.



1 - Basically, if you call your client function with an identifier, you will be able to retrieve it in your completion block:



func call(with identifier: String, at url: URL) {
URLSession.shared.dataTask(url: url) { (_, _, _) in
print(identifier)
}.resume()
}


2 - You can also use the taskIdentifier of an URLSessionDataTask. But to do this, you will need to use the delegate of your custom URLSession:



self.session = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: nil)


then you will not use a completion block but the delegate function instead:



func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(dataTask.taskIdentifier)
}


(of course you need to know which task identifier has been set for which URLSessionDataTask)



3 - If you need to access your identifier from your completion block, you can write a function which will happened it in the list of the parameter of the default completion block:



func dataTask(session: URLSession,
url: URL,
identifier: String,
completionBlock: @escaping (String, Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return session.dataTask(with: url) { (data, response, error) in
completionBlock(identifier, data, response, error)
}
}


4 - If you need to have a custom identifier in a URLSessionDataTask object, you can add it using extension and associated object:



extension URLSessionDataTask {
var identifier: String? {
get {
let identifier = objc_getAssociatedObject(self, &kIdentiferId)
if let id = identifier as? String {
return id
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &kIdentiferId, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

private var kIdentiferId: Int8 = 100


Then you can use it like this:



let task = session.dataTask(url: url)
task.identifier = "hello"





share|improve this answer













You have many ways to achieve this.



1 - Basically, if you call your client function with an identifier, you will be able to retrieve it in your completion block:



func call(with identifier: String, at url: URL) {
URLSession.shared.dataTask(url: url) { (_, _, _) in
print(identifier)
}.resume()
}


2 - You can also use the taskIdentifier of an URLSessionDataTask. But to do this, you will need to use the delegate of your custom URLSession:



self.session = URLSession(configuration: URLSessionConfiguration.default,
delegate: self,
delegateQueue: nil)


then you will not use a completion block but the delegate function instead:



func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
print(dataTask.taskIdentifier)
}


(of course you need to know which task identifier has been set for which URLSessionDataTask)



3 - If you need to access your identifier from your completion block, you can write a function which will happened it in the list of the parameter of the default completion block:



func dataTask(session: URLSession,
url: URL,
identifier: String,
completionBlock: @escaping (String, Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return session.dataTask(with: url) { (data, response, error) in
completionBlock(identifier, data, response, error)
}
}


4 - If you need to have a custom identifier in a URLSessionDataTask object, you can add it using extension and associated object:



extension URLSessionDataTask {
var identifier: String? {
get {
let identifier = objc_getAssociatedObject(self, &kIdentiferId)
if let id = identifier as? String {
return id
} else {
return nil
}
}
set {
objc_setAssociatedObject(self, &kIdentiferId, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}

private var kIdentiferId: Int8 = 100


Then you can use it like this:



let task = session.dataTask(url: url)
task.identifier = "hello"






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 15:40









Y.BonafonsY.Bonafons

1,399916




1,399916













  • Thanks. This solution worked for me.

    – Ramakrishna
    Dec 6 '18 at 9:01



















  • Thanks. This solution worked for me.

    – Ramakrishna
    Dec 6 '18 at 9:01

















Thanks. This solution worked for me.

– Ramakrishna
Dec 6 '18 at 9:01





Thanks. This solution worked for me.

– Ramakrishna
Dec 6 '18 at 9:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53519430%2fhow-to-find-which-response-belongs-to-which-request-in-async-api-calling-using-u%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks