Webリクエストをキャッシュしようとしています。基本的に私はFacebookのユーザーの友人リストを使用するアプリがありますが、ログインするたびに毎回それをつかみたくありません。ドキュメントディレクトリ内のplistでフレンドリストをキャッシュすることは、この機能には意味があるようです。私は次のようにこれを行います:NSMutableArrayをドキュメントディレクトリに書き込むことができません。
- (void)writeToDisk {
NSLog(@"writing cache to disk, where cache = %@", cache);
BOOL res = [cache writeToFile:[FriendCache persistentPath] atomically:YES];
NSLog(@"reading cache from disk immediately after writing, res = %d", res);
NSMutableArray *temp = [[NSMutableArray alloc] initWithContentsOfFile:[FriendCache persistentPath]];
NSLog(@"cache read in = %@", temp);
}
+ (NSString *)persistentPath {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"FriendCache.plist"];
}
これは基本的にNSMutableArrayをラップするFriendCacheシングルトンのメンバーです。私は、peristentPathメソッドが有効なパスを返すことを確認しました。 writeToDiskメソッドで見ることができるように、キャッシュにデータがあることを確認してから、書き込みの結果を出力し、データが読み込めるかどうかを確認します。データが読み込まれることはありません。 WRITETOFILEを使用しているとき、私はチェックアウト
一つのことがある...
2010-12-28 13:35:23.006 AppName[51607:207] writing cache to disk, where cache = (
{
birthday = "<null>";
name = "Some Name1";
pic = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs1324.snc4/7846385648654.jpg";
"pic_big" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs442.snc4/784365789465746.jpg";
"pic_square" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs1324.snc4/7846357896547.jpg";
sex = female;
status = "<null>";
uid = 892374897165;
},
{
birthday = "<null>";
name = "Some Name2";
pic = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs625.ash1/54636536547_s.jpg";
"pic_big" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs170.ash2/65465656365666_n.jpg";
"pic_square" = "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs625.ash1/654635656547_q.jpg";
sex = female;
status = "<null>";
uid = 7658436;
},
:ファイルの書き込みは、キャッシュプリントの出力が非常に長いですが、ここでは簡略版である0
です私が書いているオブジェクトに有効なplistオブジェクトがあることを確認する必要があります。私はこれをチェックして、ここで私はキャッシュオブジェクトを構築する方法ですでした:
- (void)request:(FBRequest*)request didLoad:(id)result{
NSMutableArray *friendsInfo = [[[NSMutableArray alloc] init] autorelease];
for (NSDictionary *info in result) {
NSString *friend_id = [NSString stringWithString:[[info objectForKey:@"uid"] stringValue]];
NSString *friend_name = nil;
NSString *friend_sex = nil;
NSString *friend_relationship_status = nil;
NSString *friend_current_location = nil;
if ([info objectForKey:@"name"] != [NSNull null]) {
friend_name = [NSString stringWithString:[info objectForKey:@"name"]];
}
if ([info objectForKey:@"relationship_status"] != [NSNull null]) {
friend_relationship_status = [NSString stringWithString:[info objectForKey:@"relationship_status"]];
}
if ([info objectForKey:@"sex"] != [NSNull null]) {
friend_sex = [NSString stringWithString:[info objectForKey:@"sex"]];
}
if ([info objectForKey:@"current_location"] != [NSNull null]) {
friend_current_location = [[info objectForKey:@"current_location"] objectForKey:@"name"];
}
NSString *friend_pic_square = [info objectForKey:@"pic_square"];
NSString *friend_status = [info objectForKey:@"status"];
NSString *friend_pic = [info objectForKey:@"pic"];
NSString *friend_pic_big = [info objectForKey:@"pic_big"];
NSString *friend_birthday = [info objectForKey:@"birthday"];
NSDictionary *friend_info = [NSDictionary dictionaryWithObjectsAndKeys:
friend_id,@"uid",
friend_name, @"name",
friend_pic_square, @"pic_square",
friend_status, @"status",
friend_sex, @"sex",
friend_pic, @"pic",
friend_pic_big, @"pic_big",
friend_birthday, @"birthday",
friend_relationship_status, @"relationship_status",
friend_current_location, @"current_location",
nil];
// If the friend qualifies as a single of your gender, add to the friend cache
if ([AppHelpers friendQualifies:friend_info] == YES) {
[[FriendCache sharedInstance] push:friend_info];
}
}
[[FriendCache sharedInstance] writeToDisk];
}
マイpushメソッドは、単にNSMutableArrayのプッシュをラップ:
- (void)push:(id)o {
[cache addObject:o];
}
あなたは、書き込みが失敗する理由何らかの理由を考えることができますか?
ありがとうございます!
'%@'ではなく '%d'を使って結果を' NSLog'するとどうなりますか?後者はオブジェクト用であり、BOOLでは動作しません。 '%d'は整数で、BOOLは実際には整数です。 – Joost
ああ、良いキャッチ。私は質問を更新しましたが、書き込み失敗はまだあります。 – Tony
それでは、0または1を出力しますか?私は0と思います。そして、 'NSLog(@"%@ "、キャッシュ)の出力は何ですか? – Joost