Reputation: 259
iPhone Development:
I want to have an infinite loop constantly checking an NSMutableArray and if [size > 0] pull the first object, do something with it, remove it, wait .25 seconds, then continue checking.
I want my GUI buttons to add this list. Is there a way to do this? Is there a way to do this by having the loop in the main thread?
UPDATE I guess i didn't ask the question properly. I'm using the AsyncSocket class to send telnet commands. I need to send them fast because their used to control a RC car, however the RC receiver is a tad slow. So i want to slow downs the rate in which my iPhone sends out the commands. Right now i have button events sending out the commands but i want to instead have the events add the command to a list/queue/array. In parralel to the button clicks I want a process to check the queue every .25 seconds and send out the commands in the queue.
MY PROBLEM:
the AsyncSocket isn't thread safe so I cannot have another thread sending commands. The documentation reads
If you find yourself on a thread which is different from the thread on which AsyncSocket is running, and you need to invoke a method on AsyncSocket, then you must use a method like performSelector:onThread: to ensure the method is invoked on AsyncSocket's thread (and thereby maintaining thread-safety)."
I dont know what this means...
Upvotes: 1
Views: 401
Reputation: 16540
I would recommend looking into Key Value Observing. This will allow you to set up an observing class to be notified when entries are made into the array.
It'll be a bit harder than the standard tutorials you'll find, most simply observe a property being updated. You'll need to use -willChange:valuesAtIndexes:forKey:
and -didChange:valuesAtIndexes:forKey:
when you add elements to the array. If all you need to listen for is items added, this should be enough. However, you can look into ObservingOptions to really get finer control.
See more about NSKeyValueObserving
here: http://developer.apple.com/library/ios/#documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html#//apple_ref/occ/cat/NSKeyValueObserving
This method will allow you to not waste any cycles polling for changes.
Upvotes: 4
Reputation: 1294
add an NSTimer with interval 0.25 that will be cheking it in a loop. Check documentation. There are some useful sample-projects
Upvotes: 0