我希望从具有多个调用for循环的服务器中获取数据。每次我都传递不同的参数。我知道获取数据是可能的,比如,我在下面的代码中获取:
for (NSDictionary *feedItem in [feed objectForKey:@"content"]) {
// url with feedItem data.
NSURL *url = ....
[UrlMethod GetURL:url success:^(NSDictionary *placeData) {
if (placeData) {
dispatch_async(dispatch_get_main_queue(), ^{
// adding object to table data source array
[dataSourceArray addObject:[placeData objectForKey:@"data"]];
// reloading table view.
[self.tableView reloadData];
});
}
} failure:^(NSError *error) {
}];
}
问题是,每当我向dataSourceArry添加数据时,它都不会按顺序添加。它是根据API调用的响应添加的。如果不清楚,请告诉我。
发布于 2016-12-02 22:08:39
在您的示例中,我首先分配一个可变数组,并在每个位置设置NSNull null:
NSInteger count = [[feed objectForKey:@"content"] count];
NSMutableArray *dataSourceArray = [NSMutableArray arrayWithCapacity:count];
for (NSInteger i = 0; i < count; ++i) {
[dataSourceArray addObject:[NSNull null]];
}
然后,我将使用名为调度组的内容(请参阅这里的更多http://commandshift.co.uk/blog/2014/03/19/using-dispatch-groups-to-wait-for-multiple-web-services/):
__block NSError *apiCallError = nil; // Just to keep track if there was at least one API call error
NSInteger index = 0;
// Create the dispatch group
dispatch_group_t serviceGroup = dispatch_group_create();
for (NSDictionary *feedItem in [feed objectForKey:@"content"]) {
// Start a new service call
dispatch_group_enter(serviceGroup);
// url with feedItem data.
NSURL *url = ...
[UrlMethod GetURL:url success:^(NSDictionary *placeData) {
if (placeData) {
dispatch_async(dispatch_get_main_queue(), ^{
// Add data to Data Source
// index should be the correct one, as the completion block will contain a snapshot of the corresponding value of index
dataSourceArray[index] = [placeData objectForKey:@"data"];
}
dispatch_group_leave(serviceGroup);
} failure:^(NSError *error) {
apiCallError = error;
dispatch_group_leave(serviceGroup);
}];
index++;
}
dispatch_group_notify(serviceGroup, dispatch_get_main_queue(),^{
if (apiCallError) {
// Make sure the Data Source contains no [NSNull null] anymore
[dataSourceArray removeObjectIdenticalTo:[NSNull null]];
}
// Reload Table View
[self.tableView reloadData];
});
希望它对你有用。
发布于 2016-12-02 21:43:51
这可能对你有帮助,
//keep dictionary property which will store responses
NSMutableDictionary *storeResponses = [[NSMutableDictionary alloc]init];
//Somewhere outside function keep count or for loop
NSInteger count = 0;
for (NSDictionary *feedItem in [feed objectForKey:@"content"]) {
//Find out index of feddItem
NSInteger indexOfFeedItem = [[feed objectForKey:@"content"] indexOfObject:feedItem];
NSString *keyToStoreResponse = [NSString stringWithFormat:@"%d",indexOfFeedItem];
// url with feedItem data.
NSURL *url = ....
[UrlMethod GetURL:url success:^(NSDictionary *placeData) {
if (placeData) {
//instead of storing directly to array like below
// adding object to table data source array
[dataSourceArray addObject:[placeData objectForKey:@"data"]];
//follow this
//increase count
count++;
[storeResponses setObject:[placeData objectForKey:@"data"] forKey:keyToStoreResponse];
// reloading table view.
if(count == [feed objectForKey:@"content"].count)
{
NSMutableArray *keys = [[storeResponses allKeys] mutableCopy]; //or AllKeys
//sort this array using sort descriptor
//after sorting "keys"
for (NSString *key in keys)
{
//add them serially
[dataSourceArray addObject:[storeResponses objectForKey:key]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}
} failure:^(NSError *error) {
}];
}
编辑:我给出的答案是直接写在这里的,您在实际运行这段代码时可能会遇到编译错误。
发布于 2016-12-02 21:49:31
这是因为您正在异步调用web服务,所以它不能保证按照您的请求按顺序进行响应!
现在解决办法是:
api
,就像它一次给出所有数据一样。所以,你不需要做很多的网络呼叫,它也会提高性能!completion handler
的上一个。在这种情况下,一旦您得到响应,那么只有另一个请求将被初始化,但在这种情况下,您将不得不妥协与性能!!所以第一种解决方案对我来说更好!tableView
。https://stackoverflow.com/questions/40944318
复制