How to Find which response belongs to which request in Async api calling using URLSession
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
add a comment |
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
Would you like to include the code you have tried so far?
– Kamran
Nov 28 '18 at 13:25
add a comment |
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
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
swift asynchronous request response nsurlsession
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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"
Thanks. This solution worked for me.
– Ramakrishna
Dec 6 '18 at 9:01
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
Thanks. This solution worked for me.
– Ramakrishna
Dec 6 '18 at 9:01
add a comment |
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"
Thanks. This solution worked for me.
– Ramakrishna
Dec 6 '18 at 9:01
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Would you like to include the code you have tried so far?
– Kamran
Nov 28 '18 at 13:25