3

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.

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

This all works fine.

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.

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?

4

3 回答 3

6

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;

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

@property (assign, readonly) BOOL isDataAvailable

For example:

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

Hope it helps :)

于 2015-04-12T02:47:17.297 回答
0

最后,我用 an 创建了一个单例,NSCache并在去 Parse 之前查询了这个。

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

于 2014-06-18T10:26:58.060 回答
-1

You can cache result of PFQuery like below code..And need to check for cache without finding objects in background everytime..while retrieving the image.It has some other cache policies also..Please check attached link also..

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");
}

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

Hope it helps you....!

于 2014-06-17T14:53:29.123 回答