Лекция №10

Параллелизм

Николай Морев

Блоки и параллелизм

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *bookData = @{@"name": @"Harry Potter"};
[manager POST:@"http://myserver.com/validator/book"
   parameters:bookData
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [manager POST:@"http://myserver.com/book"
             parameters:bookData
                success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    [manager GET:[@"http://myserver.com/book/" stringByAppendingString:responseObject]
                      parameters:nil
                         success:^(AFHTTPRequestOperation *operation, id responseObject) {
                             // inform the user that the book was added, move to the appropriate screen
                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             if([error.domain isEqual:NSURLErrorDomain]) {
                                 // inform the user that there was a server communication problem
                             } else {
                                 // inform the user that the book could not be added (e.g. it was already added by someone else)
                             }
                         }];
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    if([error.domain isEqual:NSURLErrorDomain]) {
                        // inform the user that there was a server communication problem
                    } else {
                        // inform the user that the book was added, however could not fetch its data
                    }
                }];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if([error.domain isEqual:NSURLErrorDomain]) {
              // inform the user that there was a server communication problem
          } else {
              // inform the user that the validation failed (i.e. the book data is not valid)
          }
      }];