views:

64

answers:

2

I have a problem with a function running before the previous one has had time to finish. According to the logs they both run but it is almost as if the function called from the AppDelegate is put onto a separate thread.

-(IBAction) checkLocation {
[AppDelegate locateMe];
[self checkForChangeAndReloadTable];    
}

The locateMe function can be called from several views which would do different things with the information. It finds the user's location and saves it to user defaults. checkForChangeAndReloadTable checks the defaults and, if the location has changed, reloads the table with new, more relevant information. It seems the change happens after it checks so this does work if the checkLocation function is called twice by the user.

Is this expected? Any ideas how to get around it?

+1  A: 

Without seeing any source code to the locateMe or checkForChangeAndReloadTable methods, it's impossible to say, but several CoreLocation methods run asynchronously, which is probably your problem.

Jeff Kelley
That sounds quite likely. Thanks. Good to know it's not just because I'm calling the function from AppDelegate.How about a function that runs the whole time and listens out for changes to the user defaults? Some sort of asynchronous timer perhaps. Does that sound like it might work? (You might be able to tell I'm quite new to this!)
Nik1777
CoreLocation has several delegate methods available. Re-think your strategy: instead of continuously checking if their are updates available, allow the delegate methods to be called when there is new information.
Jeff Kelley
+1  A: 

you can use notifications to handle asynchronous events. The basic idea is to set a notification that calls checkForChangeAndReloadTable when locateme completes

ennuikiller
Thanks. Never done that before. I am trying to track down a tutorial on google as we speak. Lots of stuff about push notifications, not so much about what you're talking about. Any suggestions about where I should go for more info?
Nik1777
Found one. http://jamesabrannan.com/wordpress/wp-content/uploads/2009/08/NSNotification.pdf
Nik1777