【问题标题】:Query for PFQueryTableViewController查询 PFQueryTableViewController
【发布时间】:2015-05-18 13:26:29
【问题描述】:

我正在将用户数据从 Parse 的 User 类加载到 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;  
}

【问题讨论】:

    标签: ios pfquery pfquerytableviewcontrolle


    【解决方案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;
    }
    

    【讨论】:

    • 感谢您的回复。编辑了上面的代码,现在它可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    相关资源
    最近更新 更多