繁体   English   中英

多个异步请求,用于在目标c中创建动态表视图

[英]Multiple async request to create a dynamic table view in objective c

我正在开发一个新的iOS应用程序。 在这个应用程序中,我有一个幻灯片菜单。 我想用异步请求动态创建此菜单。 这是一种像这样的新LinkedIn菜单:

Linkedin菜单

此菜单每十秒(或更少)自动更新表视图的一行,而在另一行中,您可以获得一些配置文件信息(名称,图片...),还可以更新消息和通知图标。

我想知道如何同时管理所有这些请求。 我打算使用AFNetworking,我认为这是最好的选择。 但我不知道如何管理多个异步请求并在表视图中设置所有日期。

我想要一个请求每十秒更新一行,其他行必须从其他请求创建,例如,第一行,我的个人资料信息(名称,图片...),第二行:商品(每次更新)十秒钟)。 第三,第四等。 朋友行与其他请求的信息。 所以最后我至少需要三个请求。 这是一个例子,但我想要的是类似的

您应该保留表视图的阴影数据结构 - 您的数据模型。 当您的异步数据进入更新模型时,然后调度块以更新主线程上的UI。

UI更新方法向表视图询问可见单元格数组。 它将这些与数据模型(使用适当的锁)进行比较,然后根据需要更新单元格内容。

通过使用表格部分,您可以更轻松地确定何时插入或删除单元格。

当用户滚动表格时,您始终会查看模型以显示要显示的内容。

这实际上取决于您想要实现的行为类型。 如果你想要每10秒钟,只需进行一次异步调用,当触发回调时,只需更新你的UiTableView内容即可。 现在你说:“如何同时管理所有这些请求?”。 事实上,你一次只能拨打电话。

如果你想同时管理多个请求,AFNetworking使它变得非常简单,juste使用:

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
                          progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                        completionBlock:(void (^)(NSArray *operations))completionBlock;

要么

- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
                                      progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                                    completionBlock:(void (^)(NSArray *operations))completionBlock;

例如:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
NSURLRequest *otherRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];

AFHTTPRequestOperation *operationForImages = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operationForImages setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    //success of images request
    self.imageDictionary = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    //manage error for this request

}];
AFHTTPRequestOperation *operationForText = [[AFHTTPRequestOperation alloc] initWithRequest:otherRequest];
[operationForText setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    //success of text request
    self.textDictionary = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    //manage error for this request

}];


[[ElCuratorAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:@[operationForImages,operationForText] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

    //track progression of requests

} completionBlock:^(NSArray *operations) {

    //all the request are completed

}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM