从ParsecachingPFFile数据
我的应用程序是一个消息传递风格的应用程序,你可以“标记”另一个用户。 (有点像twitter)。
现在,当显示该消息时,属于被标记的人的头像与该消息一起显示。
用户的头像被存储为针对PFUser对象的PFFile。
我正在加载这样的东西…
PFImageView *parseImageView = ... [taggedUser fetchIfNeededInBackgroundWithBlock:^(PFObject *user, NSError *error) { parseImageView.file = user[@"avatar"]; [parseImageView loadInBackground]; }];
这一切工作正常。
如果需要加载部分代码,大部分时间都不会触及networking,因为大部分时间都会caching用户数据。
但是,获取图像并将其放入图像视图的背景部分的负载每运行一次。 PFFile数据似乎没有任何caching。
即使下载了相同的用户的头像无数次,它仍然去networking获得它。
有没有办法让这些数据caching或这是我必须实现自己?
PFFile会自动为你caching文件,如果以前的PFQuery使用caching策略,如:
PFQuery *query = [PFQuery queryWithClassName:@"MyClass"]; query.cachePolicy = kPFCachePolicyCacheThenNetwork;
要检查PFFile是否在本地caching中,请使用:
@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 } }]; }
希望能帮助到你 :)
最后我创build了一个带有NSCache
的单例,并在进入Parse之前查询了这个单例。
现在作为一个快速停止工作。 当然,这意味着每个新的会话都要重新下载所有的图像,但现在比以前好多了。
您可以像下面的代码一样cachingPFQuery的结果。并且需要检查caching,而不必每次都在后台查找对象,而检索图像。还有一些其他的caching策略..还请检查附加的链接..
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
希望它可以帮助你….!