Caching PFFile data from Parse

走远了吗. 提交于 2019-12-12 14:06:39

问题


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?


回答1:


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 :)




回答2:


In the end I created a singleton with an NSCache and queried this before going to Parse.

Works as a quick stop for now. Of course, it means that each new session has to download all the images again but it's a lot better now than it was.




回答3:


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....!



来源:https://stackoverflow.com/questions/24265456/caching-pffile-data-from-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!