按照与 NSOperationQueue 相同的顺序接收数据
我有一个 NSOperationQueue,在其中不断添加命中服务器并带来数据的 NSOperation 对象。有没有办法按照发射的顺序存储接收到的数据?例如,假设 A、B 和 C 对象按照 ABC 的顺序添加到 NSOperationQueue 中。现在A、B、C这三个线程都会在各自对应的线程中带入数据。我想将结果以相同的顺序存储在数组中,以便数组的内容变为“结果 A”、“结果 B”和“结果 C”。 请提供此问题的解决方案或正确答案的方向。谢谢你们
编辑 - 基本上,我想设计一个自动建议组件,它从服务器获取数据(与谷歌应用程序相同),我认为 NSOperationQueue 是实现此目的的最佳方法。目前我将 maxConcurrentOperationCount 设置为 1,但我想将其设置为大于 1,因为数据来自服务器的速度非常慢。请指出这个问题的正确解决方案或方向。谢谢
I have an NSOperationQueue, in which I keep on adding NSOperation objects that hit server and brings data. Is there a way to store the received data in the order in which they were fired? For example, suppose the A, B and C objects are added in NSOperationQueue in the order ABC. Now each of the three A, B and C will bring data in their corresponding threads. I want to store the results in the same order in an array so that the contents of array becomes "Result A", "Result B" and "Result C".
Please provide a solution or a direction towards the right answer for this problems. Thanks Guys
EDITED -
Basically, I want to design an autosuggest component which brings data from server (same as in google app), and I thought NSOperationQueue to be the best method to implement this. Currently I set the maxConcurrentOperationCount to 1, but I want to set it to more than 1, as data comes from the server very slowly. Please point out the right solution or direction towards this question. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建每条数据时为其赋予一个值。您可以将索引传递到 NSOperation 类中,当您收到响应时,将数据包装在您自己的类中并为其提供索引)
,然后,当所有操作返回时,您应该拥有一个 MyData 对象数组。只需按索引对它们进行排序即可按初始顺序获取它们:) 然后提取数据应该很简单。
我假设您不想一次只进行一项操作:)
Give each piece of data a value when you create them. You can pass the index into your NSOperation class and when you get a response, wrap the data in a class of your own and give it the index)
and then, when all your operations have returned you should have an array of MyData objects. Just sort them by index to get them in the initial order :) Then extracting the data should be simple.
I've assumed that you don't want to just one operation at a time :)
您真的需要所有结果吗?我想如果您正在开发自动建议组件,您应该只对最后的结果感兴趣。如果是这种情况,您可以在添加要处理的最后一个操作之前将“-(void)cancelAllOperations”消息发送到 NSOperationQueue。这样,处于等待状态的操作将被取消,并且正在处理的操作有机会在返回数据之前检查其“取消”状态。
Do you really need all the results? I imagine that if you are developing an autosuggest component you should be interested in the last result only. If this is the case you could send the "-(void)cancelAllOperations" message to your NSOperationQueue before you add your last operation to be processed. This way the operations that are in waiting state will be canceled and the ones that are being processed have the opportunity to check their "cancel" state before they return data.
如果您一次使用 1 个操作,那么简单的
NSURLConnection
将使用NSURLRequest
和委托方法来完成此操作。无需使用操作创建操作队列更新:如果您想下载单个资源并为此使用并行下载 - 请参阅此问题/答案。
您可以为不同的 NSURLRequests/NSURLConnections 定义相同的主机,并添加额外的“Range”http 标头来定义请求文件的数据范围,而不是使用不同的主机。之后,所有操作都完成,数据被收集到数组中,只需按请求的范围对数据对象进行排序并将其连接起来即可。
If you're using 1 op at a time, then simple
NSURLConnection
would do that withNSURLRequest
and delegate methods. No need to create operation queue with operationsUPDATE: If you'd like to download single resource and use parallel downloads for this - see this question/answer.
Instead of using different hosts you'd define same host for different NSURLRequests/NSURLConnections and add extra "Range" http header to define the data range of requested file. Afterwards all operations were complete and data was gathered into, say, array, just sort data objects by Range they were requested with and concatenate it.