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

Nishit
  • 591
  • 1
  • 6
  • 17

3 Answers3

3

If you're using 1 op at a time, then simple NSURLConnection would do that with NSURLRequest and delegate methods. No need to create operation queue with operations

UPDATE: 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.

Community
  • 1
  • 1
Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • +1 for this answer. You can further optimize by setting the max thread count to be greater than one but the increase in efficiency in downloading multiple files at once seems outweighed by the logic needed to implement it + earlier iOS versions support at most 2 (more than likely just 1) – donkim Jan 05 '11 at 19:02
1

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)

@class MyData : NSObject {
    NSData *data;
    uint index;
}

@property (nonatomic, assign) uint index;
@property (nonatomic, copy) NSData *data;

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 :)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Thanks for your reply, but I am using one operation at a time. Can u please provide a solution for this with concurrentOperationCount=1? thanx – Nishit Jan 05 '11 at 16:56
1

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.

federivo
  • 103
  • 6
  • Thanks for replying. Yes, I need all the results, and I want to store the results in the same sequence as their corresponding requests have been added to the queue, i.e. if I type CAR, the server requests goes in the order C,CA,CAR and I want to store the results in the same order in array so that at index 0 results of C must be there, at index 1 results of CA and at index 2 results of CAR. Thanks – Nishit Jan 06 '11 at 13:15