我运行此代码以使用Parse.com的PFQuery
:
+ (void)fetchFresh:(BOOL)refresh threadsForUser:(PFUser *)user orItem:(PFObject *)item withCompletion:(void(^)(NSArray *array))completion {
PFQuery *queryFrom = [PFQuery queryWithClassName:THREAD];
[queryFrom whereKey:THREAD_FROM equalTo:[PFUser currentUser]];
PFQuery *queryTo = [PFQuery queryWithClassName:THREAD];
[queryTo whereKey:THREAD_TO equalTo:[PFUser currentUser]];
if (user) {
[queryFrom whereKey:THREAD_TO equalTo:user];
[queryTo whereKey:THREAD_FROM equalTo:user];
}
PFQuery *query = [PFQuery orQueryWithSubqueries:@[queryFrom, queryTo]];
[query includeKey:THREAD_FROM];
[query includeKey:THREAD_TO];
[query includeKey:THREAD_ITEM];
[query includeKey:@"item.owner"];
[query includeKey:@"item.purpose"];
[query includeKey:@"item.category"];
[query includeKey:@"item.shareContacts"];
[query includeKey:@"item.shareGroups"];
[query includeKey:THREAD_MESSAGES];
[query includeKey:THREAD_SWAP_ITEMS];
[query includeKey:@"swapItems.owner"];
[query includeKey:@"swapItems.purpose"];
[query includeKey:@"swapItems.category"];
[query includeKey:@"swapItems.shareContacts"];
[query includeKey:@"swapItems.shareGroups"];
if (item) {
[query whereKey:THREAD_ITEM equalTo:item];
}
NSLog(@"%@ %@", refresh ? @"REFRESH" : @"NO REFRESH", query.hasCachedResult ? @"CACHE" : @"NO CACHE");
[query setCachePolicy:(refresh || !query.hasCachedResult) ? kPFCachePolicyCacheThenNetwork: kPFCachePolicyCacheElseNetwork];
query.limit = 1000;
[query findObjectsInBackgroundWithBlock...];
}
这里的想法是,如果存在缓存结果,它应该转为缓存,但是在没有缓存结果时第一次获取它们。
如果NSLog
已缓存结果,我尝试query
时,结果始终为NO
。
值得一提的是,我每次都运行这个确切的代码,即每次都创建一个新的PFQuery
对象。我的理解是结果是根据PFQuery
属性的集合进行缓存的,而不是它是同一个对象,所以我希望缓存结果。
ALSO ,该方法是我用来处理数据请求的实用程序类中的类方法。这可能是一个问题吗?