I'm currently populating a UITableView with JSON data (NSArray format) from a remote webservice. I would like to speed up the application by making a call to the webservice and storing the JSON data to a local file.
also, is this a good way to do this so the user doesn't have to keep downloading data all the time?
What I'm stuck on is how to save the remote JSON file to a local file. In my -(void)saveJsonWithData:(NSData *)data
method how do I save the remote data.
Here's the code I'm using so far (from some Stackoverflow searching)
-(void)saveJsonWithData:(NSData *)data{
NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];
[data writeToFile:jsonPath atomically:YES];
}
-(NSData *)getSavedJsonData{
NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];
return [NSData dataWithContentsOfFile:jsonPath]
}
Then call the function as
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self saveJsonWithData:data];
}
thanks for help