【问题标题】:Caching PFFile data from Parse从 Parse 缓存 PFFile 数据
【发布时间】:2014-08-07 13:30:33
【问题描述】:

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

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

用户的头像存储为针对 PFUser 对象的 PFFile。

我正在加载类似这样的内容...

PFImageView *parseImageView = ...

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

这一切都很好。

如果需要,代码的加载部分大部分时间不会触及网络,因为大部分时间它都缓存了用户数据。

但是,获取图像并将其放入图像视图的后台加载部分每次都会运行。 PFFile 数据似乎根本没有任何缓存。

即使多次下载同一个用户的头像,它仍然会去网络获取它。

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

【问题讨论】:

  • 我有几乎同样的问题,我正在使用他们的缓存策略之一,但它有一些缺点。如果您检查 Anypic 的代码,您会看到另一个更高级的解决方案,他们用于点赞和 cmet,但这对我来说很难。
  • @vv88 谢谢,我去看看任意图片源代码。

标签: ios objective-c parse-platform pffile


【解决方案1】:

您可以像下面的代码一样缓存 PFQuery 的结果..并且需要检查缓存而不是每次都在后台找到对象..在检索图像时.它还有一些其他缓存策略..请检查附加链接..

PFQuery *attributesQuery = [PFQuery queryWithClassName:@"YourClassName"];
attributesQuery.cachePolicy = kPFCachePolicyCacheElseNetwork;  //load cache if not then load network
if ([attributesQuery hasCachedResult]){
    NSLog(@"hasCached result");
}else{
    NSLog(@"noCached result");
}

来源:https://parse.com/questions/hascachedresult-always-returns-no

希望对您有所帮助....!

【讨论】:

  • 谢谢,但问题不在于对象。问题是对象中包含的 PFFile 的数据。
【解决方案2】:

最后我创建了一个带有NSCache 的单例,并在去解析之前查询了这个。

现在作为一个快速停止。当然,这意味着每个新会话都必须重新下载所有图像,但现在比以前好多了。

【讨论】:

    【解决方案3】:

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

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多