我的应用程序是一个消息传递风格的应用程序,您可以在其中“标记”另一个用户。 (有点像推特)。
现在,当显示此消息时,属于被标记的人的头像会与该消息一起显示。
用户的头像存储为针对 PFUser 对象的 PFFile。
我正在加载这样的东西......
PFImageView *parseImageView = ...
[taggedUser fetchIfNeededInBackgroundWithBlock:^(PFObject *user, NSError *error) {
parseImageView.file = user[@"avatar"];
[parseImageView loadInBackground];
}];
一切正常。
如果需要,加载部分代码在大多数情况下不会接触网络,因为在大多数情况下它会缓存用户数据。
但是,获取图像并将其放入 ImageView 的后台加载部分每次都会运行。 PFFile 数据似乎根本没有任何缓存。
即使在多次下载同一用户的头像后,它仍然会去网络获取它。
有没有办法让这些数据缓存起来,或者这是我必须自己实现的东西?
最佳答案
PFFile会自动为你缓存文件,如果之前的PFQuery使用缓存策略如:
PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
要检查 PFFile 是否在本地缓存中,使用:
@property (assign, readonly) BOOL isDataAvailable
例如:
PFFile *file = [self.array objectForKey:@"File"];
if ([file isDataAvailable])
{
// no need to do query, it's already there
// you can use the cached file
} else
{
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
if (!error)
{
// use the newly retrieved data
}
}];
}
希望对您有所帮助:)
关于ios - 从 Parse 缓存 PFFile 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265456/