ios8 - 如何取消 [GMSPlacesClient autocompleteQuery] 中正在进行的请求?

标签 ios8 google-places-api google-maps-sdk-ios

我想为我的应用程序添加自动完成功能。 我的想法是使用 GMSPlacesClient 类中的 autocompleteQuery:bounds:filter:callback。 当用户输入时,我想调用该方法,但我想如果我发送多个请求,我可能会无序地收到响应。出于这个原因,我想取消当前的,但我没有办法。 也许它是在内部实现的,我不知道。 有什么帮助或建议吗?非常感谢。

我意识到响应可能会乱序。我创建了一个带有文本字段和一个表格的小示例。 每当用户点击一封信时,我都会发送一个请求,结果显示没有自动取消或订购请求。

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 Web 服务并手动取消正在进行的请求。

最佳答案

我们 Places API for 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
                      }];
}

不是最好的,但在我们发布更好的方法之前它应该可以工作:)

关于ios8 - 如何取消 [GMSPlacesClient autocompleteQuery] 中正在进行的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692599/

相关文章:

ios - 在 IOS 8 的 UIActionSheet 中添加 UIPickerView 不起作用

带有外部蓝牙键盘的 iOS 8 自定义键盘

sorting - 如何在 swift 中使用 "ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering"?

android - Google Place api for android : How to know which is address, 城市或郊区从谷歌返回

javascript - Google map 地点 API : TypeError: autocomplete is undefined

ios - 如何在 IOS 中弯曲多段线(谷歌地图)?

ios - 如何在谷歌地图 ios 中标记 map 标记

ios - 应用程序在iOS 8上崩溃并显示崩溃日志,该日志中的内容

javascript - google maps api 无法读取 placesservice

ios - 在 Xcode 中收到 "Check dependencies warning: skipping file"错误