我有一个PFQueryTableViewController设置为在加载视图时检索满足一组基本约束的对象列表下面是实现查询的方法。
- (PFQuery *)queryForTable {
//The base query.
self.query = [PFQuery queryWithClassName:self.parseClassName];
[self.query includeKey:@"photos"];
[self.query includeKey:@"user"];
[self.query orderByDescending:@"createdAt"];
[self.query whereKey:@"ordered" equalTo:[NSNumber numberWithBool:false]];
// [query whereKey:@"location" nearGeoPoint:self.currentLocation withinKilometers:32.2];
// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
self.query.cachePolicy = kPFCachePolicyNetworkOnly;
}
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
self.query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return self.query;
}
但是,我还希望为用户提供过滤结果的选项。 我用来尝试和过滤的代码不起作用,因为对象列表没有变化。
[self clear];
[self.query whereKey:@"cuisine" equalTo:cuisine.text];
[self loadObjects];
这些说明嵌入在UIAlertController
中,其中包含名为UITextField
的{{1}}
为什么过滤器没有通过?
谢谢, 亚洲时报Siddharth
答案 0 :(得分:1)
每次执行loadObjects时,它都会通过queryForTable获取一个新查询:
试试这个:
-(void)search
{
[self clear];
//include this if you want to clear the query before applying the new filter
self.query = [self startingQuery];
[self.query whereKey:@"cuisine" equalTo:cuisine.text];
[self loadObjects];
}
-(PFQuery *)query
{
if (!_query) _query = [self startingQuery];
return _query;
}
- (PFQuery *)queryForTable {
return self.query;
}
-(PFQuery *)startingQuery
{
//The base query.
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query includeKey:@"photos"];
[query includeKey:@"user"];
[query orderByDescending:@"createdAt"];
[query whereKey:@"ordered" equalTo:[NSNumber numberWithBool:false]];
// [query whereKey:@"location" nearGeoPoint:self.currentLocation withinKilometers:32.2];
// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
query.cachePolicy = kPFCachePolicyNetworkOnly;
}
// If no objects are loaded in memory, we 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;
}
return query;
}