ios - 查询 PFQueryTableViewController

标签 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;  
}

最佳答案

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

示例更正代码:

- (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;
}

关于ios - 查询 PFQueryTableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304474/

相关文章:

ios - 如何提供支持 UITableViewDataSource 协议(protocol)的继承 View Controller ?

android - 查询 Parse 数据库以从 Parse 数据类中的单个列获取数据

swift - Parse 中的数据未加载到 PFQueryTableViewController 中

ios - 解析 PFQuery IOS 返回用户名

ios - 在 Parse 数据库中查询具有多个指针的类

swift - PFQueryTableViewController 查询指向 User 类的指针并将其作为用户名标签放入表中(Swift,Parse)

ios - PFQueryTableViewController刷新数据

ios - 在 XCode 中的新 Mac 上打开项目后团队签名不起作用

ios - 使用 Cordova Push phonegap-plugin-push 1.8.0 的 PhoneGap Windows 构建失败

ios - 检查公共(public) CloudKit 数据库中记录的所有权