現在、リモート Web サービスからの JSON データ (NSArray 形式) を UITableView に入力しています。Web サービスを呼び出し、JSON データをローカル ファイルに保存することで、アプリケーションを高速化したいと考えています。
また、これは、ユーザーが常にデータをダウンロードし続ける必要がないようにするための良い方法ですか?
私が行き詰まっているのは、リモート JSON ファイルをローカル ファイルに保存する方法です。私の-(void)saveJsonWithData:(NSData *)data
方法では、リモートデータをどのように保存しますか。
これまでに使用したコードは次のとおりです(いくつかのStackoverflow検索から)
-(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]
}
次に、関数を次のように呼び出します
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self saveJsonWithData:data];
}
手伝ってくれてありがとう