skobbler can i cache json data?

ⅰ亾dé卋堺 提交于 2019-12-11 12:13:58

问题


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).



来源:https://stackoverflow.com/questions/29765992/skobbler-can-i-cache-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!