1

I have an App that is used for mobile shopping. I have a "LocationModel" singleton object that gets a list of addresses from a pList and converts each address into a "Location" object. In the location object I have a "Latitude" and "longitude" variable. Currently I have a method that iterates through the location object array and pings the google.api servers with the address inside each location object, the return datatype is then parsed using NSXML parser and the coordinates extracted and assigned to each location object.

Right now all this is done on the main thread and as such the GUI is locked when the app starts for a good couple of seconds. I would like to start a new thread in the appdelegate's main method that does this work of getting coordinates in the background. But I have never done multi threading before and I looked at the Apple concurrent programming guides and Threading guide and it seems overwhelming. Can someone please point me to some resources that are easy to understand and or have sample code.

Many thanks

banditKing
  • 9,405
  • 28
  • 100
  • 157

1 Answers1

1

You can look at this method. This is available on every NSObject subclass. This will spawn a new thread on the background and run that method. You can put all the code related to downloading and processing in the method. An important point to remember is that you can't update UI from any thread other than the main thread. So once you're ready to update the UI, you should use performSelectorOnMainThread:withObject:waitUntilDone: to call a method which will update the UI.

While this should suffice to start with, I recommend that you look at GCD.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • Thanks a lot. I actually donot need to update UI. The background thread simply gets info for the location model. Once the info is complete it flips a BOOL and then the location model knows that all coordinates are in place. WHile the user shops, this process happens in the background and if the user goes to the location display page and the BOOL is not flipped a spinner is shown... How does this sound? – banditKing May 21 '11 at 00:39