我正在尝试缓存 Web 请求。基本上我有一个使用 facebook 用户的朋友列表的应用程序,但我不想在他们每次登录时都抓住它。也许每月刷新一次。在文档目录的 plist 中缓存好友列表似乎对此功能有意义。我这样做如下:
- (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"];
}
这些是我正在使用的 FriendCache 单例的成员,它基本上包装了 NSMutableArray。我已经验证 peristentPath 方法正在返回有效路径。正如您在 writeToDisk 方法中看到的那样,我验证缓存中有数据,然后打印写入结果并检查是否可以读回任何数据。永远不会读回数据,因为结果文件写入为0。
缓存打印的输出很长,但这里是缩写版本:
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;
},
...
我检查的一件事是在使用 writeToFile 时,我必须确保我正在编写的对象具有有效的 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];
}
我的推送方法只是包装了 NSMutableArray 推送:
- (void)push:(id)o {
[cache addObject:o];
}
你能想到写入失败的任何原因吗?
谢谢!