iPhone app getting XML then refreshing it at intervals - iphone

I have an app which gets some data from the web via an XML document. I have this working fine and have followed apples SeismicXML example (uses NSURLRequest etc). I am very new to this so I have to admit that I do not totally understand all the code that gets the XML - but it is working. My problem is that my app may be running for some time so I want to be able to refresh the XML every now and again and check to see if it is different. If it is different I need to update my contents. Basically this means my questions are....
Is there a standard way of doing this?
I was thinking of creating a timer to call the function which parses the XML but I can't figure out which function to call.
If anyone can give me any pointers or even better examples of this it would be fab. Thanks

An NSTimer is the way to kick off a process that needs performing every x seconds
[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(theXMLMethod)
userInfo:nil
repeats:YES];
This sets a timer of 5 seconds that calls the theXMLMethod method.

Related

sleepForTimeInterval change interval

I use sleepForTimeInterval. My code
- (void)runningInBackground
{
while (1) {
[NSThread sleepForTimeInterval:waitInterval];
[self getInterval];
}
}
How can i change interval if sleepForTimeInterval is not finished?
You can not. No run loop processing occurs while the thread is blocked.
There are almost no cases where sleepForTimeInterval: is correct iOS code. iOS performs these kinds of things with NSTimer, NSOperation or GCD queues. If you find yourself calling NSThread, you are almost certainly in the wrong part of the framework.
Without knowing the details of your problem, the tool you probably want is an NSTimer. They're simple to use, and solving this kind of problem is easy with them. You just invalidate the timer and create a new one when you want to change the interval. You don't need to break out of a sleep.
But you should always ask yourself if you could turn a polling (interval-based) solution into an event-driven solution. What are you doing when you wake up? If you're usually just checking something and going back to sleep, that's very bad for the battery. iOS has good solutions for making those things event driven in most cases (so you just get called when the thing you want occurs without polling).

iOS: Wait for an event of button during the loop

I'm working on a small project in which I need to execute a LOOP to ask questions, and during the LOOP I need to wait for the answer from user before I can perform the next operation.
Any of you could help?
The iPhone's object-oriented framework, Cocoa Touch, already includes the loop you need. There's a class called NSRunLoop that does exactly what you require: it waits for events from the user interface (among other things) and then calls your code to handle the events.
So don't worry about building this loop yourself. Apple has a tutorial that shows how you can build an app that waits for user input and does work based on that input.
You can set all your butons into disabled state:
[myButton setUserInteractionEnabled:false];
After loop is finished, just set it back to true state. This seems to be the easiest possibility.

iPhone MultiThreading: List/Queue

I finally got my telnet iPhone application running to send text commands to a device i'm working on but the device isn't as fast as i would like it to be.
As of now when i press a button the phone sends a text command to the device's IP. I want to change it so that a constant process is running and checking a queue every .25 seconds. If the queue has any elements it will send, wait .25 seconds, and check again.
My initial guess is that I should check some iPhone threading libraries so that the buttons that add to queue and the send/checker method could be in separate threads.
I was looking at iOS Reference specifically at the Operation Queue and Dispatch Queue. Are these queues what I should be looking at or am i completely off here?
UPDATE:
I think i found what I want in NSThread however im reading NSMutableArray isnt thread safe. Is there a list type queue or vector that is thread safe in objective c?
UPDATE 2:
Could I use a mutablearray and put a lock{} around it everytime i add or remove objects?
You're on the right track. Running a task at a regular time interval sounds like a job for NSTimer. To get this behavior, try creating a timer like so:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(doTask) userInfo:nil repeats:YES];
Set this object to an instance variable and it will run -doTask every 0.25 seconds. As for your background operations, NSOperation and NSOperationQueue are probably your best bets. Create a custom NSOperation subclass and override the appropriate methods to run a task in the background.
GCD would be a better solution, NSTimers are not really that good for periodic tasks. Check the dispatch_after documentation.
Why not fire the command immediately via an asynchronous method?
Letting the framework handle the multithreading is almost always the right idea, in my experience.

Async image download for UITableViewCell will not fire connectionDidFinishLoading: until scrolling stops

I have a typical UITableView which displays a thumbnail image and some text. During tableView:cellForRowAtIndexPath: I start an async image download for each item in the list. The images are downloading, but not the way I want. For example, if I flick scroll the list, my download requests happen immediately, however, the connectionDidFinishLoading: message will not get fired, until the scrolling from the flick scroll comes to a complete stop. Basically, that results in the user seeing a bunch of empty images for a couple of seconds.
If you look at the app store app, for example, when you flick scroll one of the lists, the images begin displaying even when scrolling hasn't stopped. I'm assuming this is due to some kind of multi-threaded solution.
Can someone please provide me with an example of how I can acheive the desired results?
Thanks!
You probably need to do this :
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
Assuming _connection is your NSURLConnection.
I ended up solving this myself, since no one on the planet could help me. If you would really like to know how to achieve this, I have described how to modify Apple's LazyTableImages sample project to achieve the desired effect here:
Question about Apple's LazyTableImages sample - Doesn't behave exactly like the app store
take a look at the answer here.
lazy loading images in UITableView iPhone SDK
i use in in a few of my apps and it works great

How to avoid UI freezes?

Im trying to find the best way to avoid having tiny UI lockups after certain events in my app. For example here are two cases where i have come across mini-lockups:
I have a button that when pressed loads a local mp3 file (around 20-30mb) using AVAudioPlayer, calls the prepareToPlay method and then plays the audio file. The issue is that the button has the showsTouchWhenHighlighted flag set to yes and after pressing it it stays highlighted until the audio file begins playing which could take 1-3 secs.
Another similar example is using a navbar button to sort and reload the table data. There is a very short but noticeable lockup where the button remains highlighted until the table has reloaded.
Can anyone suggest a good approach to minimizing/eliminating these mini lockups? I didnt really come across any code to achieve this in any of the books/tutorials i read that dealt with tableviews and avaudioplayer. Should i be launching these actions on different threads?
thx
For number 1, your best bet is to load the item on a background thread and inform the user that you're doing so (either via a loading HUD or some other indicator). The simplest way of doing this will be to use - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg;. If you're running iOS 4.0 + you may want to look into executing block callbacks and see if they will work for you.
For number 2, perform the sorting on the background thread or change your sorting method (consider using a sorted data structure rather than resorting after inserts?). reloadData should occur only on the main thread.
I'm an iPhone noob myself, but it sounds like your code is stuck doing synchronous actions, which will guarantee that your UI gets locked up until the action is done executing.
Although I don't have an explicit answer, look for asynchronous options to perform these actions as they will not lockup your UI. These are usually achieved through using threads or deferred objects. From my experience so far w/ Objective-C, most actions your program neds to take can be achieved through async actions.