从 Parse 缓存 PFFile 数据 [英] Caching PFFile data from Parse

查看:30
本文介绍了从 Parse 缓存 PFFile 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个消息风格的应用程序,您可以在其中标记"另一个用户.(有点像推特).

My app is a messaging style app and in it you can "tag" another user. (A bit like twitter).

现在,当显示此消息时,属于被标记人员的头像将与该消息一起显示.

Now, when this message is displayed, the avatar belonging to the person(s) who was tagged is displayed with that message.

用户的头像作为 PFFile 存储在 PFUser 对象中.

The avatar of the user is stored as a PFFile against the PFUser object.

我正在加载类似这样的东西...

I'm loading it something like this...

PFImageView *parseImageView = ...

[taggedUser fetchIfNeededInBackgroundWithBlock:^(PFObject *user, NSError *error) {
    parseImageView.file = user[@"avatar"];
    [parseImageView loadInBackground];
}];

这一切正常.

加载(如果需要)部分代码在大多数情况下不会接触网络,因为大多数情况下它缓存了用户数据.

The load if needed part of the code will most of the time not touch the network as for the majority of the time it has the user data cached.

然而,获取图像并将其放入图像视图的背景部分的加载每次都会运行.PFFile 数据似乎根本没有任何缓存.

However, the load in background part that gets the image and puts it into the image view runs every single time. There doesn't seem to be any caching on the PFFile data at all.

即使多次下载同一个用户的头像后,它仍然会通过网络获取.

Even after downloading the same user's avatar numerous times it still goes to the network to get it.

有没有办法让这些数据缓存,还是我必须自己实现?

Is there a way to get this data to cache or is this something I'll have to implement myself?

推荐答案

PFFile 会自动为你缓存文件,如果之前的 PFQuery 使用缓存策略如:

PFFile will automatically cache the file for you, if the previous PFQuery uses caching policy such as:

PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;

要检查 PFFile 是否在本地缓存中,请使用:

To check whether the PFFile is in local cache, use:

@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
        }
    }];
}

希望有帮助:)

这篇关于从 Parse 缓存 PFFile 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆