1

我正在将用户数据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;  
}
4

1 回答 1

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;
}
于 2015-05-18T13:35:26.787 回答