nsurlsessiondatatask

NSURLSession doesn't return data in first call

此生再无相见时 提交于 2020-01-14 05:37:08
问题 In general, it is necessary to implement a class for the network. This is a class that will take a URL, and to give data. All this is done in order not to score an extra logic controllers. I encountered such a problem that when you first create a View, the data do not come. That's Network Class: private static var dataTask: NSURLSessionDataTask? private static var dataJSON: NSData? private static var sessionConfig: NSURLSessionConfiguration = { var configuration = NSURLSessionConfiguration

How to aggregate response from multiple NSURLSessionDataTasks?

依然范特西╮ 提交于 2020-01-05 03:11:20
问题 I am trying to aggregate the data from multiple NSURLSessionDataTasks that will run concurrently. __block NSMutableDictionary *languageDetails = [NSMutableDictionary new]; [repos enumerateObjectsUsingBlock:^(NSDictionary *repoDict, NSUInteger idx, BOOL * _Nonnull stop) { NSString *languageUrl = repoDict[@"languages_url"]; NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:languageUrl] completionHandler:^(NSData * _Nullable data, NSURLResponse *

Is there a way to request multiple distinct resources in parallel using URLSession.shared.dataTask

十年热恋 提交于 2020-01-01 19:44:30
问题 I found this piece of code here on how to download images simultaneously without any breakages, func loadImageRobsAnswer(with urlString: String?) { // cancel prior task, if any weak var oldTask = currentTask currentTask = nil oldTask?.cancel() // reset imageview's image self.image = nil // allow supplying of `nil` to remove old image and then return immediately guard let urlString = urlString else { return } // check cache if let cachedImage = DataCache.shared.object(forKey: urlString) { self

How to get NSDictionary from NSURLSessionDataTask

风流意气都作罢 提交于 2019-12-24 18:06:07
问题 -(NSDictionary *)fetchFromUrl:(NSString *)url{ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { dataFetched = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; }]; [task resume]; NSLog(@"dataFetched, %@", dataFetched); return dataFetched; } So I

Call performSegue after URLSession.shared.dataTask

一世执手 提交于 2019-12-24 05:46:07
问题 I want to performe a Segue after sending a post request using URLSession.shared.dataTask ViewController: @IBAction func Connection(_ sender: AnyObject) { let loginFunc = Login() loginFunc.login(username: username.text!, password: password.text!) { jsonString in let response = jsonString print(response) if response.range(of: "failure") == nil { self.performSegue(withIdentifier: "home", sender: nil) } } } Login: class Login { // the completion closure signature is (String) -> () func login

iOS background task using NSURLSessionDataTask

寵の児 提交于 2019-12-22 00:40:39
问题 I have flight search feature in my application which is taking too long to get the data (more than 25 seconds). if application goes background or went to sleep mode, internet connection get disconnected. I have written below logic using apple example to make api request keep going even though if app goes to background but it's not working. self.session = [self backgroundSession]; self.mutableData = [NSMutableData data]; NSURL *downloadURL = [NSURL URLWithString:@"http://jsonplaceholder