Reputation: 6469
I'm using Alamofire to send HTTP request in my App. I'm using a TabBarViewController, In first view's ViewDidLoad, I send a request. Also in ViewWillDisappear, I send another request. However, I found it behave unexpected when I changing the tabs.
func sendHttpCommand(parameter: NSDictionary) {
Alamofire.request(.GET, URL, parameters: (parameter as! [String: AnyObject]))
.response {
request, response, data, error in
print(request)
}
}
viewDidLoad() {
let dict: NSDictionary = ["value": 0]
sendHttpCommand(dict)
}
viewWillDisappear(animated: Bool) {
let dict: NSDictionary = ["value": 1]
sendHttpCommand(dict)
}
When I switching the tabs, in NORMAL CASE, my console will print out
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
However, when I switching the tabs fast enough, my console will print out
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=1})
Optional(NSMutableURLRequest {URL: xxxxx/value=0})
Any ideas?
Upvotes: 0
Views: 502
Reputation: 71
the alamofire requests are executed async.
Read here to understand asyncs and syncs: Difference between dispatch_async and dispatch_sync on serial queue?
You can cancel your alamofire request when you change the tab and the request is not finished. For this you need an Alamofire Manager.
Upvotes: 2