-1

I have the following code below that loops through an array. I need to check if the finish or fail selector has been called iterating to the next object in my dataArray.

        for (id object in dataArray) {

            [client setDidFinishSelector:@selector(getDataFinish:)];
            [client setDidFailSelector:@selector(getDataFail:)];
            [client getData:object];
        }

In my getDataFinish method I assign values and I am trying to keep it in order. If I use the above method, the values can get out of order since the client response time can be different for each request..

Danger Veger
  • 1,117
  • 1
  • 9
  • 16
  • 1
    What sort of data are you handling? - I think its generally bad practice to do multiple requests like this - If you can, you should think about restructuring your endpoint to accept multiple values and return them too. – Wez Apr 02 '15 at 15:38
  • 1
    I would add the operations in a NSOperationQueue instead – Pablo A. Apr 02 '15 at 15:41
  • your requests could have a concept of previous request and next request. If a request completes, don't do anything unless the previous request has completed. Also, always try to handle the next request, unless it's not completed. – nielsbot Apr 02 '15 at 16:01
  • You could also use an NSOperationQueue. Modelling your requests as NSOperation allows them to have dependencies. I.e., require one request to finish before another one can. (This is really the same as my last comment) – nielsbot Apr 02 '15 at 16:02

1 Answers1

0

I see two possible solutions, depending on what you're actually trying to do. It sounds like you're making calls to the internet, so yes you will get varied response time (or no response at all). Because of this, I would recommend using NSNotification. See this answer for more information about that.

Another option is making a flag in your code (AKA a BOOL) that you set to YES when your method has completed. Again, if you're making calls to the web I would not recommend this method as you are setting yourself up for an infinite loop if the user has no service and the BOOL never changes.

If you are still having trouble let me know and I can provide a more detailed answer.

Community
  • 1
  • 1
Max Friedman
  • 445
  • 4
  • 15