user1285402
user1285402

Reputation: 163

How to save multiple xml files in cache file path in ios?

Im saving xml file in cache file path as follows:

// Determile cache file path
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 filePathl = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"list.xml"];   

// Download and write to file
 NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
 NSData *urlData = [NSData dataWithContentsOfURL:url];
 [urlData writeToFile:filePathl atomically:YES];

But using this code i can retrive data of recently used xml file.Could anybody please tell me how to save multiple xml files for offline use?

Upvotes: 0

Views: 548

Answers (1)

Midhun MP
Midhun MP

Reputation: 107201

You need to save the xml with different names. Else it'll overwrite old xml.

Keep a integer value for this purpose, if you want this after the app restart also keep it in NSUserDefaults.

 int posValue = [[[NSUserDefaults standardUserDefaults] objectForKey:@"lastXml"] intValue];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 filePathl = [NSString stringWithFormat:@"%@/list_%d.xml", [paths objectAtIndex:0],posValue];   

 NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
 NSData *urlData = [NSData dataWithContentsOfURL:url];
 [urlData writeToFile:filePathl atomically:YES];

 [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:posValue+1] forKey:@"lastXml"];

Upvotes: 1

Related Questions