确保异步方法按照它们被调用的顺序执行
假设我有3个异步方法,它们都做同样的事情:将一些数据发送到服务器并将响应数据添加到数组(所有这些方法都添加到相同的阵列)。确保异步方法按照它们被调用的顺序执行
我在同一时间调用这些方法,但他们发送不同数量的数据到服务器。
如果第一个方法比第三个方法向服务器发送更多的数据,第三个方法将尽快返回响应,从而更快地将数据添加到数组中。响应数据取决于坐标,所以它们在数组中的顺序很重要。
我该如何确定,即使第三个方法比第一个或第二个方法获得repsonse更快,在前面的方法之前它不会将数据添加到数组中?因此,保持数组中坐标的顺序。
方法是NSURLConnection
s,它们都发送异步请求。
编辑:这里是工作代码:
//Array of snapped points from the JSON
NSArray *snappedPoints = [result objectForKey:@"snappedPoints"];
NSMutableArray *locationsArray = [[NSMutableArray alloc] init];
//Loop through the snapped points array and add each coordinate to a temporary array
for (int i = 0; i<[snappedPoints count]; i++) {
NSDictionary *location = [[snappedPoints objectAtIndex:i] objectForKey:@"location"];
double latitude = [[location objectForKey:@"latitude"] doubleValue];
double longitude = [[location objectForKey:@"longitude"] doubleValue];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[locationsArray addObject:loc];
}
//Add these temporary location arrays to the dictionary with the key as the request number
[tempLocationDictionary setObject:locationsArray forKey:[NSString stringWithFormat:@"%i",requestNumber]];
//If all requests have completed get the location arrays from the dicitonary in the same order as the request were made
if([tempLocationDictionary count] == numberOfRequests){
//Just a path because I am drawing these coordinates on a map later
GMSMutablePath *path = [GMSMutablePath path];
//Loop through the dictionary and get the location arrays in the right order
for (int i = 0; i<[tempLocationDictionary count]; i++) {
//Create a dummy array
NSArray *array = [tempLocationDictionary objectForKey:[NSString stringWithFormat:@"%i",i+1]];
//Get the coordinates from the array which we just got from the dictionary
for (CLLocation *location in array) {
[path addCoordinate:location.coordinate];
}
}
一种方法是用NSURLSession
及其相关类来代替NSURLConnection
。这是更新的,通常是更好的选择。对于您的具体情况,可以使用自定义NSURLSessionConfiguration
,您将HTTPMaximumConnectionsPerHost
设置为1.这样,连接将被迫按顺序运行,从而解决您的问题。
当然,没有同时连接可能会减慢速度。在这种情况下,您必须临时累积数据而不是将其添加到您的阵列,并且只在所有连接完成时更新阵列。根据服务器返回的数据,有多种方法可以做到这一点。
一个比较简单的方法:如果你知道你总是有三个连接,使用一个NSMutableDictionary
对象作为整数NSNumber
对象。每个连接后你会做这样的事情
mutableDictionary[@1] = // your server data here
使用@2
或@3
为其他连接。每次添加数据时,请检查是否有三种结果,如果有,请将所有数据添加到阵列中。有很多的解决这个其他方式,关键是有某种临时搭建在那里你可以(一)积累数据,直到所有的连接完成的,和(b)跟踪,其中从连接出来的数据,或者简单按编号,或通过URL,或由服务器提供的其他独特数据。
这个答案的后半部分是我想到的,为什么我要求OP发布一些相关的代码。然后可以提供更具体的解决方案。 – rmaddy
谢谢!我在脑海里也有类似的东西。 (b)跟踪哪些数据来自哪个连接。我想过为每个连接分配一个数字(每个连接增加一个数字),并用连接分配的数字将他们的数据添加到字典中。根据数字从字典中将数据添加到数组中,从数字1开始。这会起作用吗? –
是的,这个想法确实起作用。我更新了代码以反映这一点!再次感谢! –
这是NSOperationQueue和依赖项需要解决的问题。
退房Advanced NSOperations [transcript]从2015年WWDC:
当WWDC应用程序启动时,有设置的一堆,我们需要做的。
首先,我们要下载一个小的配置文件,该文件会告诉我们的小东西像什么是最最新支持版本的应用程序,有什么特点,我们已经启用,等等。
因此,在我们下载此文件后,我们将执行版本检查以确保您运行的是最新版本的WWDC应用程序。
然后在我们检查应用程序的版本后,我们可以开始下载有用的信息,例如我们在新闻标签中显示的新闻以及会议日程安排。
在我们下载完日程表后,我们可以开始导入您保存到iCloud的任何收藏夹,您提交的任何反馈意见以便您可以在应用程序中看到它,并且我们也将开始下载视频列表。
应用这些操作描述的依赖关系应该很好地排序。
取决于这些方法正在做什么使它们异步。这些'NSURLSession'调用? 'NSOperationQueue'?还有别的吗? –
NSURLConnection与异步请求! –
用相关的代码更新你的问题,这样人们就可以指引你一个好的方向。 – rmaddy