How can I cancel the current request in [GMSPlacesClient autocompleteQuery]?

I would like to add an autocomplete function to my application. My idea is to use autocompleteQuery: bounds: filter: callback from the GMSPlacesClient class. While the user is typing, I would like to name this method, but I assume that if I send multiple requests, I might get the answers out of order. For this reason, I would like to cancel the current one, but I do not see the way. Maybe it is implemented domestically, I do not know. Any help or suggestion? Many thanks.

I realized that the answers could fail. I created a small sample with a text box and one table. I sent a request every time the user deletes the letter, and the results indicate that there is no automatic cancellation of the request or order.

2015-11-13 15:16:14.668 TestGooglePlaces[5233:60b] u
2015-11-13 15:16:15.550 TestGooglePlaces[5233:60b] ut
2015-11-13 15:16:15.700 TestGooglePlaces[5233:60b] uto
2015-11-13 15:16:15.967 TestGooglePlaces[5233:60b] utop
2015-11-13 15:16:16.552 TestGooglePlaces[5233:60b] utopi
2015-11-13 15:16:23.035 TestGooglePlaces[5233:60b] Results for u
2015-11-13 15:16:23.079 TestGooglePlaces[5233:60b] Results for utop
2015-11-13 15:16:23.087 TestGooglePlaces[5233:60b] Results for utopi
2015-11-13 15:16:23.093 TestGooglePlaces[5233:60b] Results for ut
2015-11-13 15:16:23.155 TestGooglePlaces[5233:60b] Results for uto

? , , - REST .

+2
1

API- Places iOS- . SDK , .

, , :

@implementation YourClass {
  NSUInteger _sequenceNumber;
  __block NSUInteger _mostRecentResponseNumber;
}

- (void)autocompleteQuery:(NSString *)query {
  NSUInteger thisSequenceNumber = ++_sequenceNumber;
  [_placesClient autocompleteQuery:query
                            bounds:nil
                            filter:nil
                          callback:^(NSArray * _Nullable results, NSError * _Nullable error) {
                            if (thisSequenceNumber <= _mostRecentResponseNumber) {
                              return;
                            }
                            _mostRecentResponseNumber = thisSequenceNumber;
                            // process results here
                      }];
}

, , :)

+4

Source: https://habr.com/ru/post/1615274/


All Articles