0

I need to setup some sort of loop that runs until an array has data in it. A little bit of background information: the application talks to the server and gets some information and populates that information into an array. Only when that information is populated into the array can the view get changed to the next view (because the view is populated with information from the array).

How would I create this loop? I'm currently using an NSTimer but that is not suitable for my needs.

Thanks in advance!

6
  • 1
    Why are you busy-waiting on that array? Why not just have the same block of code that fills the array notify your controller that the data is ready? Commented Aug 16, 2011 at 16:19
  • 2
    I would try to set up an asychronus callback with the server if possible. Setting up a loop can be expensive Commented Aug 16, 2011 at 16:20
  • Yan - That array has information that populates labels/etc in the next view. How would I have it notify me that the data is ready, etc? Commented Aug 16, 2011 at 16:24
  • i second Kocharyan, you should do an async call then populate when u get data. Commented Aug 16, 2011 at 16:24
  • Unfortunately I'm using a post, which I have heard before can't be done using async. If it can please let me know. Commented Aug 16, 2011 at 16:26

3 Answers 3

1

When you say looping until I assume you really mean wait until.

First of if this waiting is to be done on the main thread just forget about it, never block the main thread.

Instead of a loop you probably want to use a lock, and wait for the condition. This requires a shared lock between the code where you wait for the array to populate, and the code where you populate the array.

First create a shared condition lock like this:

typedef enum {
    MYConditionStateNoObjects,
    MYConditionStateHaveObjects
} MYConditionState;
...
sharedArray = [[NSMutableArray alloc] init];
sharedLock = [[NSConditionLock alloc] initWithCondition:MYConditionStateNoObjects];

Your method that populates the array should then do:

[sharedLock lockWhenCondition:MYConditionStateNoObjects];
// Your stuff to get the objects to add here.
[sharedArray addObjectsFromArray:theObjectsToAdd];
[sharedLock unlockWithCondition:MYConditionStateHaveObjects];

And the receiver that should wait until the array has objects do this:

[sharedLock lockWhenCondition:MYConditionStateHaveObjects];
// ... Do something with the objects you got here
[sharedArray removeAllObjects];
[sharedLock unlockWithCondition:MYConditionStateNoObjects];
Sign up to request clarification or add additional context in comments.

Comments

0

You can try using the following libraries: http://allseeing-i.com/ASIHTTPRequest/How-to-use and look for the asynchronous part

Asynchronous means that you won't block the main thread and wait for a response to come back.

3 Comments

Downvoting without any apparent reason is lame.
Not sure why you were downvoted, I haven't even looked into your response yet.
Probably someone lame. Anyways from what i understan you are basically pulling data off the web then trying to populate it to some kind of table? One way is to use an aynchronous request, look at the connection received method. You can populate your array there and then notify your table to reload the data.
0

If I got the idea, you need to handle the moment, when data from server was added to the array. So why you don't want to use KVO functionality? It will allow you to add observer which will listen for array content change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.