AFNetworking 和自动建议
我正在为我的 iOs 应用程序使用 AFNetworking。 我需要像许多其他应用程序或网站上的搜索栏一样实现搜索建议。 因此,基本上会启动 GET 请求,但在用户点击新字符时取消旧请求。 我怎样才能做到这一点?我有一个 AFHTTPClient 子类,并且正在使用 getPath。 最好的办法是让这个请求可以取消,并且“优先”到我的单例 AFHTTPClient 子类上的任何其他 HTTP 请求。 提前致谢。
I'm using AFNetworking for my iOs app.
I need to implement a search-suggest like in many other apps or search bars on websites.
So basically launch GET requests but cancelling old ones as users tap new chars.
How can I do that? I have an AFHTTPClient subclass and I'am using getPath.
The best would be to have this requests be cancellable and "prioritized" to any other HTTP request on my singleton AFHTTPClient subclass.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在 AFHTTPClient 示例中使用此方法:
如果您在 AFHTTPClient 子类上有一个单例,
Just use this method in your AFHTTPClient
Sample if you've a singleton on a AFHTTPClient subclasse:
我相信,当您尝试创建基于特定服务的应用程序时,使用 AFNetworking 中最好的 HTTPClient,该应用程序将向同一主机上的不同路径发出请求。
对于类似的东西,我会使用普通的 AFHTTPRequestOperation,它是可以取消的。我将创建一个包含 AFHTTPRequestOperation 的包装类。然后,在每次用户按下时,我都会对 http 请求(和我的包装器)调用“取消”,然后在同一位置创建一个新请求,并发出新请求。这就是我已经在我的应用程序中实现它的方式,并且效果很好。
不过要小心。 AFNetworking 很大程度上基于 NSOperation 块,当调用 cancel 时,您必须确保 NSOperation 的完成块不会被调用(或者至少在被调用后立即返回)。即使您取消操作,完成块仍有可能被调用,从而造成内存泄漏并且难以跟踪错误。最好的办法是创建一个完成块并在继续之前检查操作是否被取消。
I believe that the best HTTPClient in AFNetworking is used when you are trying to create an application based on a specific service, that will make requests to different paths on the same host.
For stuff like that, I would use the plain AFHTTPRequestOperation, that is cancelable. I would make a wrapper class with an AFHTTPRequestOperation inside. Then, on each user press, I would call 'cancel' on the http request (and my wrapper), and then create a new one in the same place, and make the new request. That's the way I have already implemented it in my apps, and it works fine.
Be careful though. AFNetworking is heavily NSOperation block based, and when calling cancel, you must make sure that the NSOperation's completion block will not get called (or at least, will return immediately after it gets called). Even if you cancel the operation, there's a chance that the completion block will still be called, thus creating memory leaks and hard to track bugs. Your best bet is to create a completion block and check if the operation is cancelled before proceeding.