Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将许多请求排队,然后根据添加的时间进行排序,但项目最终根据从请求接收到它们的时间进行排序_Ios_Objective C_Core Data_Afnetworking_Nsfetchedresultscontroller - Fatal编程技术网

Ios 将许多请求排队,然后根据添加的时间进行排序,但项目最终根据从请求接收到它们的时间进行排序

Ios 将许多请求排队,然后根据添加的时间进行排序,但项目最终根据从请求接收到它们的时间进行排序,ios,objective-c,core-data,afnetworking,nsfetchedresultscontroller,Ios,Objective C,Core Data,Afnetworking,Nsfetchedresultscontroller,我正在使用和使用一个操作队列(类似于NSOperationQueue)将一组请求排入队列 我基本上有一个URL数组,我根据每个URL创建单独的请求,将每个请求排队,然后批量执行请求。它们一次一个地返回,我给它们加上时间戳,然后将它们添加到核心数据中,核心数据是我的表视图的源,因此表视图被填充 仅当项目成功返回时,才会发出时间戳。这意味着项目是根据返回的时间排序的,而不是按照原始URL数组的顺序排序的。因此,返回时间较短(API处理的文本较少)的请求会更早地加上时间戳,因此会被视为比稍后加上时间戳

我正在使用和使用一个操作队列(类似于NSOperationQueue)将一组请求排入队列

我基本上有一个URL数组,我根据每个URL创建单独的请求,将每个请求排队,然后批量执行请求。它们一次一个地返回,我给它们加上时间戳,然后将它们添加到核心数据中,核心数据是我的表视图的源,因此表视图被填充

仅当项目成功返回时,才会发出时间戳。这意味着项目是根据返回的时间排序的,而不是按照原始URL数组的顺序排序的。因此,返回时间较短(API处理的文本较少)的请求会更早地加上时间戳,因此会被视为比稍后加上时间戳的项目更早,因为这些项目的文本量较大。就正确的顺序而言,情况显然不是这样

最后,尽管我有API调用的URL数组,但我的表视图会根据API返回项目的时间进行排序

这是我的密码:

- (void)addArticlesFromURLs:(NSArray *)URLs fromSource:(NSString *)source {
    // Restrict amount of operations that can occur at once
    [[AFClearReadClient sharedClient].operationQueue setMaxConcurrentOperationCount:3];

    // Create an array to hold all of our requests to make
    NSMutableArray *requestOperations = [[NSMutableArray alloc] init];

    for (NSString *URL in URLs) {       
        // Create the request from the article's URL
        NSMutableURLRequest *request = [[AFClearReadClient sharedClient] requestWithMethod:@"GET" path:[NSString stringWithFormat:@"/v1/clear?url=%@&format=json", URL] parameters:nil];

        // Create the request operation and specify behaviour on success and failure
        AFHTTPRequestOperation *requestOperation = [[AFClearReadClient sharedClient] HTTPRequestOperationWithRequest:request
                                                                      success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                                            // Get the item NSDictionary from the JSON responseObject
                                                                            NSDictionary *item = [responseObject objectForKey:@"item"];

                                                                            // Get the values needed to create an article
                                                                            NSString *title = [item objectForKey:@"title"];
                                                                            NSString *URL = [item objectForKey:@"link"];
                                                                            NSString *body = [item objectForKey:@"description"];

                                                                            // Replace HTML entities with their actual characters
                                                                            title = [title stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

                                                                            // Remove all HTML and formatting from body so only plain-text remains
                                                                            body = [self removeHTMLAndFormatting:body];

                                                                                // Add it to CoreData if there's actual content (easiest way to tell is checking body)
                                                                            if (![body isEqualToString:@""]) {
                                                                                NSManagedObjectContext *context = self.managedObjectContext;
                                                                                ArticleInfo *articleInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ArticleInfo" inManagedObjectContext:context];
                                                                                articleInfo.source = source;
                                                                                articleInfo.body = body;
                                                                                articleInfo.title = title;
                                                                                articleInfo.url = URL;
                                                                                articleInfo.timeStamp = [NSDate date];

                                                                                NSError *error;
                                                                                [context save:&error];
                                                                            }
                                                                      }
                                                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                            NSLog(@"Request operation error: %@", error);
                                                                      }];

        // Save the request operation in an NSArray so all can be enqueued later
        [requestOperations addObject:requestOperation];
    }

    // Enqueue the request operations
    [[AFClearReadClient sharedClient] enqueueBatchOfHTTPRequestOperations:requestOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        [self.tableView reloadData];
    } completionBlock:^(NSArray *operations) {
        [self.tableView reloadData];
    }];
}

我该如何修改它,使我的tableview中的项目具有正确的(最新的)顺序

使用
userInfo

AFHTTPRequestOperation
子类
AFURLConnectionOperation
,以及
AFURLConnectionOperation
声明了一个
userInfo
字典,该字典可以通过而不被触摸。它的目的是让您在操作完成时添加所需的任何数据。将操作添加到队列之前,请执行以下操作:

[requestOperation setUserInfo:@{ @"timestamp" : [NSDate date] };
把这个放在你的完成块中

NSDate *timestamp = [operation userInfo][@"timestamp"];

时间戳将反映操作添加到队列的顺序。

我真的不知道
AFNetworking
,但是…

您可以在创建请求之前获取时间戳(在for循环中):

然后在操作成功块中:

...
articleInfo.url = URL;
articleInfo.timeStamp = stamp;
...
块将保留您的时间戳并将其设置为成功

...
articleInfo.url = URL;
articleInfo.timeStamp = stamp;
...