0

I have an application which lists some states (e.g Assam, Andhra, Pradesh etc). When a state is selected, the next view show hospitals located in that particular state in a table view. The hospital names are loaded from a database using web service. This is done so far, no worries.

My problem is: Suppose I have listed hospitals for a state. I go back and select the same state again, the web service is called again and user has to wait for the data. So this is not a good idea. I don't want to call the web service again if I select the same state. How to store data and display it if state selection is same?

Jonathan King
  • 1,528
  • 14
  • 25
user1523344
  • 123
  • 1
  • 1
  • 8

4 Answers4

1

You can use NSURLRequestReturnCacheDataElseLoad as the cachePolicy of NSURLRequest.

As Apple stated:

NSURLRequestReturnCacheDataElseLoad Specifies that the existing cache data should be used to satisfy a URL load request, regardless of its age or expiration date. However, if there is no existing data in the cache corresponding to a URL load request, the URL is loaded from the origin source.

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
0

to a void calling web service each time , put the result you get from WS in an 2D NSMutableArray attribute of the AppDelegate ,
and each time the user select a state ,verify if the array corresponding to your state is nil

touti
  • 1,164
  • 6
  • 18
  • it mean NSMutableArray inside an NSMutableArray the first one for the stats and the other for the hospitals – touti Aug 08 '12 at 11:18
0

Also SQLite/CoreData may be an approach you might want to consider storing data for a longer and permanent basis.

If currentDate != serverLastUpdatedDate then you can download data from server, you can store data in the form a plist for each rowtext for a quicker implementation. Have a look at the below ways of implementing it.

plist Setting up a plist to store application data (not settings) for an iPhone game

SQLite http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application

CoreData http://developer.apple.com/library/ios/#DOCUMENTATION/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html

Community
  • 1
  • 1
Lalith B
  • 11,843
  • 6
  • 29
  • 47
0

Save your data in NSUserDefault

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:hospitalAry forKey:@"Hospital"];
    [defaults synchronize];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74