Drop Request
In most of the time user switch between one page to another or one controller to another using tab bar icon without waiting for its result. due to this reason multiple network request ongoing and application becomes slow, it’s better to dismiss all its previous network request and keep only current network request so we can get the data fast. here I have taken the example of URLSession request:
1: Define the global class where we have to make a network request and with the help of delegate method, we can get the data to the respective class.
2: Lets we have a global class like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
func callingHttpPostMethod (params:String,apiname : String ,signalName: String){ let defaults = UserDefaults.standard; let urlString = BASE_DOMAIN + apiname print("url %@",urlString); print("params %@",params); //queue.async { var request = URLRequest(url: URL(string: urlString)!) request.httpMethod = "POST" let postString = params; request.httpBody = postString.data(using: .utf8) if defaults.object(forKey: "authKey") == nil{ request.addValue("", forHTTPHeaderField: "authKey") request.addValue(API_USER_NAME, forHTTPHeaderField: "apiKey") request.addValue(API_KEY, forHTTPHeaderField: "apiPassword") } else{ request.addValue(defaults.object(forKey: "authKey") as! String, forHTTPHeaderField: "authKey") request.addValue(API_USER_NAME, forHTTPHeaderField: "apiKey") request.addValue(API_KEY, forHTTPHeaderField: "apiPassword") } task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error print("error=\(String(describing: error))", "aaa",error) if let error = error as NSError?, error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled { } else{ DispatchQueue.main.async { self.sendingErorReport(); } } return } if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors print("statusCode should be 200, but is \(httpStatus.statusCode)") print("response = \(String(describing: response))") DispatchQueue.main.async { self.sendingErorReport(); } } do { let json = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions()) as (AnyObject) collectionData = json as AnyObject if(signalName.compare("HttpLoginPostMetod") == .orderedSame){ collection = collectionData as! NSDictionary let defaults = UserDefaults.standard; defaults .set(collection["sessionId"], forKey: "sessionId") DispatchQueue.main.async { self.startCallingApi(); } } else if(signalName.compare("HttpPostMetod") == .orderedSame){ DispatchQueue.main.async { self.startFinalData(); } } } catch { print("json error: \(error)") DispatchQueue.main.async { self.sendingserverErrorReport(); } } } task.resume() //} } |
Note:
a: here task is a global variable.
b: if let error = error as NSError?, error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled { }
This one detects when we cancel the previous task.
If this will execute we will nothing to do otherwise we are maintaining the server error response.
3: in every view controller we are doing this:
1 2 3 4 5 |
override func viewWillDisappear(_ animated: Bool) { SVProgressHUD.dismiss() globalObjectAddReview.cancelNetworkRequest() } |
4: define this method in the global class where we have written the network request.
1 2 3 4 |
func cancelNetworkRequest(){ print("cancelled") task.cancel() } |
5: Now this will cancel the previous request and load the current request.