以前に行われたHTTPリクエストを回避するためにCachéを使用するSOAPWebサービスに取り組んでいます。
NSDictionaryをファイルに保存しようとすると、エラーが発生し(プロパティリストの構造に従わないためだと思います)、修正方法を知っています。
これは私のコードです:
[self loadCache];
NSData *responseData;
NSMutableDictionary* dict = [_cache objectForKey:delegate.type];
if (dict != nil && [dict objectForKey:request.HTTPBody] != nil ) {
responseData = [dict objectForKey:request.HTTPBody];
} else {
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
if (dict == nil) {
dict = [[NSMutableDictionary alloc] init];
[_cache setObject:dict forKey:delegate.type];
[dict setObject:[NSNumber numberWithDouble:NSTimeIntervalSince1970] forKey:@"FECHA"];
}
if (responseData == nil) {
responseData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"]];
}
NSLog(@"%@", [[request.HTTPBody copy] class]);
[dict setObject:responseData forKey:request.HTTPBody];
[self saveCache];
}
loadCache関数:
- (void)loadCache {
if (_loadedCache) return;
NSString* docsurl = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
docsurl = [docsurl stringByAppendingPathComponent:@"cache.txt"];
_cache = [[NSMutableDictionary alloc] initWithContentsOfFile:docsurl];
if (_cache == nil) _cache = [[NSMutableDictionary alloc] init];
}
saveCache関数:
- (void)saveCache {
NSString* docsurl = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
docsurl = [docsurl stringByAppendingPathComponent:@"cache.txt"];
if ([_cache writeToFile:docsurl atomically:YES]) {
NSLog(@"SAVED");
} else {
NSLog(@"ERROR");
}
}