Parse.com查询:缓存始终为空(iOS) [英] Parse.com Query: Cache is always empty (iOS)

查看:82
本文介绍了Parse.com查询:缓存始终为空(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Parse.com作为后端编写iOS-App.

I'm writing an iOS-App using Parse.com as a backend.

在我的PFQueryTableViewController- (PFQuery)queryForTable方法中,我从Parse中检索了一组数据,但是当设备当前处于脱机状态时,我无法缓存该查询以支持功能.

In the - (PFQuery)queryForTable method of my PFQueryTableViewController I'm retrieving a set of data from Parse, but I'm unable to cache this query in order to support functionality when the device is currently offline.

方法如下:

- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"city" equalTo:[[NSUserDefaults standardUserDefaults] objectForKey:@"city"]];

// userMode is active when a user is logged in and is about to edit coins
if (self.userModeActive) {
    [query whereKey:self.textKey equalTo:self.user[@"location"]];
}

// dateFilter is active when view is pushed from an event
if (self.dateFilterActive) {
    [self createDateRangeForFilter];
    [query whereKey:@"date" greaterThan:[[self createDateRangeForFilter] objectAtIndex:0]];
    [query whereKey:@"date" lessThan:[[self createDateRangeForFilter] objectAtIndex:1]];
} else {
    // Add a negative time interval to take care of coins when it's after midnight
    [query whereKey:@"date" greaterThanOrEqualTo:[[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 6)]];
    [query orderByAscending:self.dateKey];
}

// locationFilter is active when view is pushed from a location profile
if (self.locationFilterActive) {
    [query whereKey:@"location" equalTo:self.locationToFilter];
}

// If no objects are loaded in memory, look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}

if ([query hasCachedResult]) {
    NSLog(@"hasCache");
} else {
    NSLog(@"chache empty");
}

return query;

}

[query hasCachedResults]在这种情况下总是返回false.

[query hasCachedResults] always returns false in this case.

在另一个类中,我正在执行几乎完全相同的查询(在不同的Parse-Class上),并且它会自动缓存.唯一的区别是,此另一个查询包含PFFiles.

In another class, I'm doing the almost exactly same query (on a different Parse-Class) and it caches automatically. The only difference is, that this other query contains PFFiles.

这可能是一个愚蠢的问题,但我已经坚持了好几天.

This may be a dumb question but I'm stuck with it for days now.

感谢您的帮助,如果可以为您提供更多信息,请告诉我.

Thanks for any help and let me know if I can give you more information.

推荐答案

代码使用条件if (self.objects.count == 0)保护高速缓存策略的设置.似乎有零个对象时您正在使用缓存,而在查询成功之后就不使用它.由于默认设置是不使用缓存,因此代码安排为永远不使用它.

The code guards the setting of cache policy with a conditional if (self.objects.count == 0). It would seem that you're using the cache when there are zero objects and not using it after the query has succeeded. Since the default is to not use the cache, the code is arranged to never use it.

只需删除条件,或根据[query hasCachedResult]

Just remove the conditional, or use the cache conditionally upon [query hasCachedResult]

EDIT -仍然可以/应该无条件设置缓存策略,但是只有在查找后查询的条件不变的情况下,查询才能具有hasCachedResults看到文档中的某个位置对此进行了确认,但这是有道理的).为了确保查询可以返回缓存的结果,请在查找后保留其条件不变.

EDIT - It is still the case that the cache policy can/should be set unconditionally, but a query can have hasCachedResults only if its criteria don't change after the find (I don't see a place in the docs confirming this, but it stands to reason). To insure that a query can return cached results, leave its criteria unchanged after the find.

这篇关于Parse.com查询:缓存始终为空(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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