A
A
Alexander Zarochintsev2014-04-07 12:18:57
Objective-C
Alexander Zarochintsev, 2014-04-07 12:18:57

Obj-C How to work with streams correctly?

In general, this is the situation, I use the AFNetworking library, I execute a POST request - authorization, the request goes fine, but when the request is in progress and the user clicks the "Login" button again , strange things happen, since the previous request did not have time to work out, and then there is more one, I certainly understand that it is possible to create a BOOL variable, and when the request is in process, do not call the authorization method, but it is very long, especially when there are many methods, how else can this error be avoided? Surely there are worked out schemes, maybe I searched very badly - but I did not find a solution to the situation on Google .
Request example:

- (IBAction)login:(UIButton *)sender {
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  NSDictionary *parameters = @{@"_login": @"bar", @"_pass" : @"123"};
  [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"JSON: %@", responseObject);
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error: %@", error);
  }];
}

Thank you for your attention! I hope to find a solution here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Morozov, 2014-04-07
@Kovalskiy

in this particular case you just need to disable the button

- (IBAction)login:(UIButton *)sender {
        sender.enabled = NO;
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  NSDictionary *parameters = @{@"_login": @"bar", @"_pass" : @"123"};
  [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSLog(@"JSON: %@", responseObject);
            sender.enabled = YES;
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"Error: %@", error);
            sender.enabled = YES;
  }];
}

In general, the task that you describe in my opinion is specific to each project.
if you want to execute requests of the same type sequentially, you can use, for example, NSOperationQueue
if you want only one request of a specific type to be executed at a time, then in any case set a BOOL flag, you can write a wrapper over your method above that will handle such situations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question