running a nested Alamofire request synchronously
I am trying to retrieve data from my node by making requests multiple times (to get the distance a car travels). However it seems that the executiono is not occuring in the right order. I believe this is due to the fact that the requests are not processed synchronously.
I have looked at other questions but it doesnt seem to fit my situation because of the nested requests.
var d1: Double!
var d2: Double!
var d3: Double!
var d4: Double!
var speed: Double!
var speed1: Double!
@objc func getSpeed() {
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d1 = response["distance"] as? Double
print("First distance is (self.d1) KM")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d2 = response["distance"] as? Double
print("The second distance is ((self.d2)!)")
let distance = self.d2 - self.d1
print("The total distance for the first part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the first part")
}
}
})
}
}
}
sleep(30)
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d3 = response["distance"] as? Double
print("The third distance is ((self.d3)!)")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d4 = response["distance"] as? Double
print("The fourth distance is ((self.d4)!)")
let distance = self.d4 - self.d3
print("The total distance for the second part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the second part")
}
}})
}
}
}
if speed1 == 0 && speed > 90 {
//Wait 5 minutes to see if the car moves
sleep(30)
if speed1 == 0 {
//Send SMS
Alamofire.request("(Constants.appServer)/message?long=longitude&lat=lat", method: .get).responseJSON { _ in}
}
}else {
print("There is no issue")
}
}
I am running the below function every 5 minutes or so in my appdelegate,
swift alamofire
|
show 1 more comment
I am trying to retrieve data from my node by making requests multiple times (to get the distance a car travels). However it seems that the executiono is not occuring in the right order. I believe this is due to the fact that the requests are not processed synchronously.
I have looked at other questions but it doesnt seem to fit my situation because of the nested requests.
var d1: Double!
var d2: Double!
var d3: Double!
var d4: Double!
var speed: Double!
var speed1: Double!
@objc func getSpeed() {
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d1 = response["distance"] as? Double
print("First distance is (self.d1) KM")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d2 = response["distance"] as? Double
print("The second distance is ((self.d2)!)")
let distance = self.d2 - self.d1
print("The total distance for the first part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the first part")
}
}
})
}
}
}
sleep(30)
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d3 = response["distance"] as? Double
print("The third distance is ((self.d3)!)")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d4 = response["distance"] as? Double
print("The fourth distance is ((self.d4)!)")
let distance = self.d4 - self.d3
print("The total distance for the second part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the second part")
}
}})
}
}
}
if speed1 == 0 && speed > 90 {
//Wait 5 minutes to see if the car moves
sleep(30)
if speed1 == 0 {
//Send SMS
Alamofire.request("(Constants.appServer)/message?long=longitude&lat=lat", method: .get).responseJSON { _ in}
}
}else {
print("There is no issue")
}
}
I am running the below function every 5 minutes or so in my appdelegate,
swift alamofire
2
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (as! [String: Any]?
). If you want to conditional downcast the proper syntax isas? [String: Any]
– vadian
Jan 20 at 12:21
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Please search forDispatchGroup
for example stackoverflow.com/questions/52126494/…
– vadian
Jan 20 at 12:36
|
show 1 more comment
I am trying to retrieve data from my node by making requests multiple times (to get the distance a car travels). However it seems that the executiono is not occuring in the right order. I believe this is due to the fact that the requests are not processed synchronously.
I have looked at other questions but it doesnt seem to fit my situation because of the nested requests.
var d1: Double!
var d2: Double!
var d3: Double!
var d4: Double!
var speed: Double!
var speed1: Double!
@objc func getSpeed() {
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d1 = response["distance"] as? Double
print("First distance is (self.d1) KM")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d2 = response["distance"] as? Double
print("The second distance is ((self.d2)!)")
let distance = self.d2 - self.d1
print("The total distance for the first part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the first part")
}
}
})
}
}
}
sleep(30)
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d3 = response["distance"] as? Double
print("The third distance is ((self.d3)!)")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d4 = response["distance"] as? Double
print("The fourth distance is ((self.d4)!)")
let distance = self.d4 - self.d3
print("The total distance for the second part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the second part")
}
}})
}
}
}
if speed1 == 0 && speed > 90 {
//Wait 5 minutes to see if the car moves
sleep(30)
if speed1 == 0 {
//Send SMS
Alamofire.request("(Constants.appServer)/message?long=longitude&lat=lat", method: .get).responseJSON { _ in}
}
}else {
print("There is no issue")
}
}
I am running the below function every 5 minutes or so in my appdelegate,
swift alamofire
I am trying to retrieve data from my node by making requests multiple times (to get the distance a car travels). However it seems that the executiono is not occuring in the right order. I believe this is due to the fact that the requests are not processed synchronously.
I have looked at other questions but it doesnt seem to fit my situation because of the nested requests.
var d1: Double!
var d2: Double!
var d3: Double!
var d4: Double!
var speed: Double!
var speed1: Double!
@objc func getSpeed() {
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d1 = response["distance"] as? Double
print("First distance is (self.d1) KM")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d2 = response["distance"] as? Double
print("The second distance is ((self.d2)!)")
let distance = self.d2 - self.d1
print("The total distance for the first part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the first part")
}
}
})
}
}
}
sleep(30)
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON { (response) in
if let result = response.result.value as! [String:Any]? {
if let response = result["data"] as! [String: Any]?
{
self.d3 = response["distance"] as? Double
print("The third distance is ((self.d3)!)")
//Delay for 5 secods
sleep(5)
//Make the call again.
Alamofire.request("(Constants.appServer)/speed", method: .get).responseJSON(completionHandler: { (response) in
if let result = response.result.value as! [String: Any]? {
if let response = result["data"] as! [String:Any]?
{
self.d4 = response["distance"] as? Double
print("The fourth distance is ((self.d4)!)")
let distance = self.d4 - self.d3
print("The total distance for the second part is (distance)")
self.speed = (distance / 5) * 1000
print("((self.speed)!) is the speed (m/s) for the second part")
}
}})
}
}
}
if speed1 == 0 && speed > 90 {
//Wait 5 minutes to see if the car moves
sleep(30)
if speed1 == 0 {
//Send SMS
Alamofire.request("(Constants.appServer)/message?long=longitude&lat=lat", method: .get).responseJSON { _ in}
}
}else {
print("There is no issue")
}
}
I am running the below function every 5 minutes or so in my appdelegate,
swift alamofire
swift alamofire
asked Jan 20 at 12:17
omaromar
24
24
2
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (as! [String: Any]?
). If you want to conditional downcast the proper syntax isas? [String: Any]
– vadian
Jan 20 at 12:21
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Please search forDispatchGroup
for example stackoverflow.com/questions/52126494/…
– vadian
Jan 20 at 12:36
|
show 1 more comment
2
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (as! [String: Any]?
). If you want to conditional downcast the proper syntax isas? [String: Any]
– vadian
Jan 20 at 12:21
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Please search forDispatchGroup
for example stackoverflow.com/questions/52126494/…
– vadian
Jan 20 at 12:36
2
2
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (
as! [String: Any]?
). If you want to conditional downcast the proper syntax is as? [String: Any]
– vadian
Jan 20 at 12:21
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (
as! [String: Any]?
). If you want to conditional downcast the proper syntax is as? [String: Any]
– vadian
Jan 20 at 12:21
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Please search for
DispatchGroup
for example stackoverflow.com/questions/52126494/…– vadian
Jan 20 at 12:36
Please search for
DispatchGroup
for example stackoverflow.com/questions/52126494/…– vadian
Jan 20 at 12:36
|
show 1 more comment
0
active
oldest
votes
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%2f54276331%2frunning-a-nested-alamofire-request-synchronously%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54276331%2frunning-a-nested-alamofire-request-synchronously%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
2
That's horrible. Please don't do that. There are APIs like Grand Central Dispatch which are able to maintain the order of asynchronous network requests. And please don't force unwrap an optional to an optional (
as! [String: Any]?
). If you want to conditional downcast the proper syntax isas? [String: Any]
– vadian
Jan 20 at 12:21
Unfortunately, I have to use Alamofire. This is for a small project, nothing major, is there no work around?
– omar
Jan 20 at 12:27
You can use GCD together with Alamofire. GCD is a mechanism to control threads and tasks
– vadian
Jan 20 at 12:31
Can you please illustrate how I would go about editing my code.
– omar
Jan 20 at 12:34
Please search for
DispatchGroup
for example stackoverflow.com/questions/52126494/…– vadian
Jan 20 at 12:36