I'm having problem while caching query. Below is the code I am using for my query. If i comment out [query whereKey:#"login" equalTo:currentUser.login] the query is caching fine.
//Defining the query
+(PFQuery *)queryForListingForCurrentUser{
PFQuery *query = [Listings query];
BOOL hasCache = [query hasCachedResult];
NSLog(hasCache ? #"yes": #"no");
User *currentUser = (User *)[PFUser currentUser];
[query whereKey:#"login" equalTo:currentUser.login];
query.cachePolicy = kPFCachePolicyNetworkElseCache;
return query;
}
//Calling the query
PFQuery *query = [Listings queryForListingForCurrentUser];
[query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error){
}];
Related
We have a Table name userProfile which has a pointer userId to User Table.
We are applying inner query on userProfile
PFQuery *query = [PFQuery queryWithClassName:#"UserProfile"];
[query whereKey:#"userId" equalTo:[PFUser currentUser]];
[query whereKey:#"relation" equalTo:#"Agent"];
[query includeKey:#"userId"];
[query includeKey:#"profileId.userId"];
query.skip = arrProfiles.count;
query.limit = 15;
NSString *searchText = [NSString stringWithFormat:#"%#",self.searchBar.text];
**PFQuery *innerQuery =[PFUser query];
[innerQuery whereKey:#"username" matchesRegex:[NSString stringWithFormat:#"(?i)%#",searchText]];
[query whereKey:#"userId" matchesQuery:innerQuery];**
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
}];
but we are unable to get any results after applying inner query.
In the query, you are saying that the userId has to be equal to the current user. But in the inner query, you are saying that the userId has to match the user with a username entered in the search field.
This means that the query has to satisfy BOTH constraints in order to return results. This can only happen if the current user is literally searching for themselves.
If you are looking for BOTH the current user's profile AND the searched-for profile, you need to use an or query like so:
PFQuery *firstQuery = [PFQuery queryWithClassName:#"UserProfile"];
[query whereKey:#"userId" equalTo:[PFUser currentUser]];
NSString *searchText = [NSString stringWithFormat:#"%#",self.searchBar.text];
PFQuery *innerQuery =[PFUser query];
[innerQuery whereKey:#"username" matchesRegex:[NSString stringWithFormat:#"(?i)%#",searchText]];
PFQuery *secondQuery = [PFQuery queryWithClassName:#"UserProfile"];
[secondQuery whereKey:#"userId" matchesQuery:innerQuery];
PFQuery *query = [PFQuery orQueryWithSubqueries:[firstQuery, secondQuery]];
[query whereKey:#"relation" equalTo:#"Agent"];
[query includeKey:#"profileId.userId"];
query.skip = arrProfiles.count;
query.limit = 15;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
}];
I was trying this
PFQuery *allDealsQuery = [Deal query];
PFRelation *favoritedDealsRelation = [user objectForKey:#"favoritedDeals"];
PFQuery *favoritedDealsQuery = [favoritedDealsRelation query];
PFRelation *redeemedDealsRelation = [user objectForKey:#"redeemedDeals"];
PFQuery *redeemedDealsQuery = [redeemedDealsRelation query];
[allDealsQuery whereKey:#"objectId" doesNotMatchKey:#"objectId" inQuery:favoritedDealsQuery];
//Parse does not support more than 2 where queries???
[allDealsQuery whereKey:#"objectId" doesNotMatchKey:#"objectId" inQuery:redeemedDealsQuery];
[allDealsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
onSuccess(objects);
}
else {
onError(error);
}
}];
When I tried to use two queries, the answer was different from what was expected.
So should I use be using only one doesNotMatchKey query?
Try referencing parse documentation on Compound Queries:
https://parse.com/docs/ios_guide#queries-compound/iOS
Compound Queries
If you want to find objects that match one of several queries, you can use orQueryWithSubqueries: method. For instance, if you want to find players with either have a lot of wins or a few wins, you can do:
PFQuery *lotsOfWins = [PFQuery queryWithClassName:#"Player"];
[lotsOfWins whereKey:#"wins" greaterThan:#150];
PFQuery *fewWins = [PFQuery queryWithClassName:#"Player"];
[fewWins whereKey:#"wins" lessThan:#5];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[fewWins,lotsOfWins]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains players with lots of wins or only a few wins.
}];
i'm new to parse and i'm trying to fetch a user objectId, but whatever i cant seem to return the objectId. I can easily return the username. this is my query:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
NSLog(#"%#", [objects lastObject]);
}
}
}];
This returns something like this:
<PFUser:9dcc65tsdr:(null)> {
username = AEleQFdBx9jdtypfsQmLtzAvW;
}
How do i return the objectId which is between PFUser and (null)?
You can use object.objectId like this:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:[[PFUser currentUser] objectForKey:#"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
for (PFObject *object in objects){
NSLog(#"Object ID: %#", object.objectId);
}
}
}
You can also use this: [[PFUser currentUser]objectId] much faster and works much better because you don't have to run a whole PFQuery.
Hello to everyone in the query I posted I'm trying to retrieve all of the posts and PfUserCurrent all posts of people who have shared a friendship with PfUserCurrent.
The query works fine but I have only one problem, the query shows me all the posts of Friends of the CurrentUser but does not show me those sent by the CurrentUser ... I've tried several attempts but I could not fix this ... Can you explain where I'm wrong?
-(void)QueryForPost {
PFQuery *QueryForFriend=[PFQuery queryWithClassName:#"Friendships"];
[QueryForFriend whereKey:#"To_User" equalTo:[PFUser currentUser]];
[QueryForFriend whereKey:#"STATUS" equalTo:#"Confirmed"];
PFQuery *QueryYES = [PFQuery queryWithClassName:#"Post"];
[QueryYES whereKey:#"FLASH_POST" equalTo:[NSNumber numberWithBool:YES]];
[QueryYES whereKey:#"UserSelected" equalTo:[PFUser currentUser]];
PFQuery *QueryNO = [PFQuery queryWithClassName:#"Post"];
[QueryNO whereKey:#"FLASH_POST" equalTo:[NSNumber numberWithBool:NO]];
[QueryNO whereKey:#"Author" matchesKey:#"From_User" inQuery:QueryForFriend];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[QueryYES,QueryNO]];
[query includeKey:#"Author"];
[query orderByDescending:FF_CREATEDAT];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
NSLog(#"%#", results);
ArrayforPost = [[NSMutableArray alloc] init];
for (PFObject *object in results) {
[ArrayforPost addObject:object];
}
[self.FFTableView reloadData];
}
}];
}
After a long discussion with #rory, here we come to the correct answer:
-(void)QueryForPost {
PFQuery *QueryForFriend=[PFQuery queryWithClassName:#"Amicizie"];
[QueryForFriend whereKey:#"A_User" equalTo:[PFUser currentUser]];
[QueryForFriend whereKey:#"STATO" equalTo:#"Confermato"];
[QueryForFriend includeKey:#"Da_User"];
PFQuery *QueryYES = [PFQuery queryWithClassName:#"Post"];
[QueryYES whereKey:#"FLASH_POST" equalTo:[NSNumber numberWithBool:YES]];
[QueryYES whereKey:#"Scelti" equalTo:[PFUser currentUser]];
PFQuery *normalPostByFriends = [PFQuery queryWithClassName: #"Post"];
[normalPostByFriends whereKey: #"FLASH_POST" equalTo: [NSNumber numberWithBool: NO]];
[normalPostByFriends whereKey: #"Utente" matchesKey:#"Da_User" inQuery:QueryForFriend];
PFQuery *normalPostByUser = [PFQuery queryWithClassName:#"Post"];
[normalPostByUser whereKey: #"FLASH_POST" equalTo: [NSNumber numberWithBool: NO]];
[normalPostByUser whereKey: #"Utente" equalTo: [PFUser currentUser]];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[QueryYES,normalPostByFriends,normalPostByUser ]];
[query includeKey:#"Author"];
[query orderByDescending:FF_CREATEDAT];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
NSLog(#"%#", results);
ArrayforPost = [[NSMutableArray alloc] init];
for (PFObject *object in results) {
[ArrayforPost addObject:object];
}
[self.FFTableView reloadData];
}
}];
}
The problem is handled by making another query to display current user's normal post and modify QueryForFriend for a correct query
I have a Parse class called FriendRelation. This class has two users, one a friend the other a user.
I want to get all of the messages posted by all of the friends of a user. I am attempting to do so with the following query:
PFQuery *innerQuery = [PFQuery queryWithClassName:#"FriendRelation"];
[innerQuery whereKey:#"user" equalTo:currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Message"];
[query whereKey:#"userMessage" matchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
}];
This query comes back with no results.
I believe this is occurring because of the line:
[query whereKey:#"userMessage" matchesQuery:innerQuery];
The where key needs to be a FriendRelation to match. Is this correct?
How can I make the results of the inner query be a user that will intern match the matching query?
Thanks!
You can try using
- (void)whereKey:(NSString *)key matchesKey:(NSString *)otherKey inQuery:(PFQuery *)query
Something like:
PFQuery *innerQuery = [PFQuery queryWithClassName:#"FriendRelation"];
[innerQuery whereKey:#"user" equalTo:currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Message"];
[query whereKey:#"userMessage" matchesKey:#"firendUser" inQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
//do something useful..
}];
Let me know how it goes!