I am loading user data from User
class from Parse into PFQueryTableViewController
. User class represents data from Facebook. Now, I do not want to load the data into PFQueryTable
of currently logged in User. All other users data is allowed to be loaded. This is my query but still it loads all data from User class. Any suggestions?
- (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;
}
The reason this is happening is, you are setting this constraint [query whereKey:@"FacebookID" notEqualTo:[[PFUser currentUser] objectForKey:@"FacebookID"]]
before initializing your query. Move the line below the initialization code, and it will work.
Sample corrected code:
- (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;
}
Alternative:
All Parse classes have a unique key, the objectId
. I suggest you use this as a constraint in your query.
- (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;
}