Multiple async request to create a dynamic table v

2019-09-18 15:28发布

问题:

I am developing a new iOS app. In this app, I have a slide menu. I want to create this menu dynamically with async requests. It's a kind of new LinkedIn menu like this one :

Linkedin Menu

This menu updates one of the rows of the table view automatically every ten seconds (or less) and in other row you have some of your profile information (name , picture...) and also you can update messages and notification icons.

I would like to know how to manage all of these requests at the same time. I am going to use AFNetworking, I think it is the best option. But I do not know how to manage multiple async requests and set all the date in the table view.

I want that one of the row update every ten seconds from a request, other rows have to be created from other requests, For example, first row, My profile info(name, picture...), second row: offers (updating every ten seconds). Third, fourth and so on. Friends rows with their info from another request. So finally I need at least three requests. This is an example, but what I want it is similar

回答1:

You should keep a shadow data structure of your table view - your data model. When your asynchronous data comes in update the model, then dispatch a block to update the UI on the main thread.

The UI update method asks the table view for the array of visible cells. It compares these to the data model (with proper locks) and then updates cell content as needed.

By using table section you may have an easier time determining when to insert or delete cells.

When the user scrolls the table you always look at the model for what content to display.



回答2:

It really depends on what kind of behaviour you want to implement. If you want every 10 seconds, just make an async call and when the callback is triggered just update your UiTableView content. Now you say: "how to manage all of these requests at the same time?". In fact you are only making a call at a time.



回答3:

If you want to manage multiple request at the same time, AFNetworking made it quite simple, juste use :

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

or

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

Exemple :

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

}];