如何在[GMSPlacesClient autocompleteQuery]中取消正在进行的请求? [英] How can I cancel an ongoing request in [GMSPlacesClient autocompleteQuery]?

查看:187
本文介绍了如何在[GMSPlacesClient autocompleteQuery]中取消正在进行的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中添加自动填充功能。
我的想法是使用autocompleteQuery:bounds:filter:来自GMSPlacesClient类的回调。
当用户输入时,我想调用该方法,但我想如果我发送了多个请求,我可能会收到无序响应。出于这个原因,我想取消现有的,但我没有看到一种方法。
也许它是在内部实现的,我不知道。
任何帮助或建议?非常感谢。

I would like to add autocomplete feature to my app. My idea is to use autocompleteQuery:bounds:filter:callback from GMSPlacesClient class. While user is typing, I would like to call that method, but I guess if I send several requests, I could receive responses out of order. For that reason I would like to cancel the current one, but I don't see a way. Maybe it is implemented internally, I don't know. Any help or suggestion? Thanks a lot.

我意识到答案可能会失灵。我用文本字段和一个表创建了一个小样本。
我每次用户点击一封信时都会发出请求,结果显示没有自动请求取消或订单。

I realised responses could come out of order. I created a small sample with a textfield and one table. I sent a request every time user taps a letter and the results say that there is no automatic request cancelation 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 Web服务并手动取消正在进行的请求。

How can I fix that problem? The only idea I have is to use the REST web service and cancel the ongoing requests manually.

推荐答案

我们在Places API上iOS团队已经意识到这个问题并且正在努力解决它。在SDK的下一个版本中,我们将有一个类来负责管理这些请求并以正确的顺序返回它们。

We on the Places API for iOS team are aware of this problem and are working on it. In the next release of the SDK we'll have a class which takes care of managing these requests and returning them in the correct order.

在此期间,您可以管理这些请求通过跟踪请求进入的顺序并忽略响应(如果它们太旧):

In the meantime, you can manage these requests by keeping track of the order that the requests came in and ignoring responses if they're too old:

@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
                      }];
}

不是最好的,但它应该有效,直到我们发布更好的做法这个:))

Not the nicest, but it should work until we release a better way of doing this :)

这篇关于如何在[GMSPlacesClient autocompleteQuery]中取消正在进行的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆