Parse.com Query for PFUser CurrentUser - ios

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

Related

Query a table in Back4app

I have a User table in Back4app. I would like to query the table. I am using the below set of statement. It does not work. Please advise. The count always comes to zero.
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
NSLog(#"%lu", objects.count);
PFObject *obj = [objects firstObject];
NSLog(#"%#", obj);
NSString *str=[obj valueForKey:#"password"];
NSLog(#"%#", str);
}];
PFQuerys for PFUsers should be instantiated via [PFUser query]
So, you should replace this line
PFQuery *query = [PFQuery queryWithClassName:#"User"];
with this line
PFQuery *query = [PFUser query];
Hope this helps :)

Unable to apply inner query on PFUser Class in Parse

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) {
}];

IOS Parse include key query

I am trying to print the name of a category using Parse in iOS and Objectve-C. Thanks for looking. Let me know if you have any questions.
CategoryInBudget Table
category(pointer)
amount(number)
user(pointer)
Category Table
name(string)
Here is my code:
PFQuery *query = [PFQuery queryWithClassName:#"CategoryInBudget"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query includeKey:#"Category"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object[#"Category"][#"name"];
}
}];

Parse Caching Issue IOS

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){
}];

Search Feature Parse IOS

I would like to make a search feature that queries two different key from the same class. What kind of regular expression could I use that return the most accurate values. Example:
- (void)searchTableList {
[self.results removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:#"TC"];
CLLocationCoordinate2D coordinate = [self.delegate.location coordinate];
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude
longitude:coordinate.longitude];
PFQuery *query1 = [PFQuery queryWithClassName:#"TC"];
[query1 setLimit:1000];
[query1 whereKey:#"location"
nearGeoPoint:geoPoint
withinKilometers:5];
[query1 whereKey:#"courses" matchesRegex:[NSString stringWithFormat:#"%#",[self.searchBar.text lowercaseString]]];
[query1 whereKey:#"owner" notEqualTo:[PFUser currentUser].username];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for( PFObject * object in objects)
{
[self.results addObject:object];
}
[query setLimit:1000];
[query whereKey:#"location"
nearGeoPoint:geoPoint
withinKilometers:5];
[query whereKey:#"owner" matchesRegex:[NSString stringWithFormat:#"%#",[self.searchBar.text lowercaseString]]];
[query whereKey:#"owner" notEqualTo:[PFUser currentUser].username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for( PFObject * object in objects)
{
[self.results addObject:object];
}
[self.tableView reloadData];
}];
}];
}
How could I take these two queries and order them in such a way that the most accurate information has priority.
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.
}];

Resources