查询PFQueryTableViewController

时间:2015-05-18 13:26:30

标签: ios pfquery pfquerytableviewcontrolle

我正在将User类的用户数据从Parse加载到PFQueryTableViewController。用户类表示来自Facebook的数据。现在,我不想将数据加载到当前登录用户的PFQueryTable。允许加载所有其他用户数据。这是我的查询,但它仍然加载来自User类的所有数据。有什么建议吗?

- (PFQuery *)queryForTable
{ 
PFQuery *query;
//[query whereKey:@"FacebookID" notEqualTo:[[PFUser currentUser] objectForKey:@"FacebookID"]];
//[query whereKey:@"username" notEqualTo:[PFUser currentUser].username];
if (self.canSearch == 0) {
    query = [PFQuery queryWithClassName:@"_User"];
    [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId];
} else {
    query = [PFQuery queryWithClassName:@"_User"];
    [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId];
    [query whereKey:@"username" matchesRegex:_searchbar.text];
}
[query orderByAscending:@"createdAt"];
// 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;  
}

1 个答案:

答案 0 :(得分:1)

发生这种情况的原因是,您在初始化查询之前设置此约束[query whereKey:@"FacebookID" notEqualTo:[[PFUser currentUser] objectForKey:@"FacebookID"]] 。将行移动到下面的初始化代码,它将起作用。

示例更正后的代码:

- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"FacebookID" notEqualTo:[[PFUser currentUser] objectForKey:@"FacebookID"]];
//[query whereKey:@"FacebookID" notEqualTo:[PFUser currentUser]];
if (self.canSearch != 0) {
    [query whereKey:@"username" matchesRegex:_searchbar.text];
}
[query orderByAscending:@"createdAt"];
// 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;
}

<强>替代:

所有Parse类都有一个唯一的密钥objectId。我建议你在查询中使用它作为约束。

- (PFQuery *)queryForTable {

    PFQuery *query = [PFUser query]; // [PFUser query] is same as [PFQuery queryWithClassName@"_User"]
    [query whereKey:@"objectId" notEqualTo:[PFUser currentUser].objectId];
    [query whereKey:@"username" matchesRegex:[NSString stringWithFormat:@"^%@", _searchBar.text] modifiers:@"i"]; // Case insensitive search

    // ...other code...

    return query;
}