skobbler can i cache json data?

2019-08-02 19:20发布

问题:

I'm using skobbler and skmaps for an app that download for offline use some regions of the map. I'm using the code i have found in the example of the framework package, in this case

MapJSONViewController
MapDownloadViewController

I have implemented also the app delegate code, so every time i start the app, it download and parse a json of about 1mb

- (void)mapsVersioningManager:(SKMapsVersioningManager *)versioningManager loadedWithMapVersion:(NSString *)currentMapVersion
{
    [[XMLParser sharedInstance] downloadAndParseJSON];
}

It's possible to avoid this behaviour? I don't want to download 1mb of json data every app init if not necessary... Maybe i can download and include a physic map json file in my app to have a start version ? Or this "local behaviour" will bring my app to work with an outdated json version very soon? Maybe another behaviour is to maintain a local version with a data and redownload it only once a week for example... It seems at me a common problem, there's someone how achive a convenient behaviour?

回答1:

Yes, you can include the json file in your app & read it from disk.

In the XMLParser.m replace the code in downloadAndParseJson with:

- (void)downloadAndParseJSON
{
    [self parseJSON];

    NSString *libraryFolderPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@",libraryFolderPath);
}

and parseJSON with:

- (void)parseJSON
{
    NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Maps" ofType:@"json"] encoding:NSUTF8StringEncoding error:nil];

    SKTMapsObject *skMaps = [SKTMapsObject convertFromJSON:jsonString];

    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate setSkMapsObject:skMaps];

    self.isParsingFinished = YES;
    [[NSNotificationCenter defaultCenter]postNotificationName:kParsingFinishedNotificationName object:nil];
}

Here you can find a modified demo project that reads the Maps.json file from resources (the .json file is included in the resources folder).