I have a requirement where i will be fetching data from a table using multiple 'OR' conditions in multiple columns, like either data in a column has to match or data in another column has to match for retrieving a row. And once this condition satisfied i need all the eligible objects to be ordered in their creation date(createdAt).
Here is what i am doing:
PFQuery *query1=[PFQuery queryWithClassName:#"Messages"];
[query1 whereKey:#"Receiver" equalTo:#"some-id"];
PFQuery *query2=[PFQuery queryWithClassName:#"Messages"];
[query2 whereKey:#"ThreadID" equalTo:#"some-threadID"];
PFQuery *finalQuery=[PFQuery orQueryWithSubqueries:#[query1,query2]];
[finalQuery orderByDescending:#"createdAt"];
NSArray *msgs=[query findObjects];
I am getting the messages but they are not lates, i.e. they are not sorted by 'createdAt'.
Can Anybody Help? Thanks
Related
I have integrated Parse in my iOS apps.
I want to know how can I set integer value in where query as below;
int userID =[[NSUserDefaults standardUserDefaults]valueForKey:#"USER_ID"];
PFQuery *querypush = [PFQuery queryWithClassName:#"EndCall"];
[querypush whereKey:#"UserId" equalTo:userID];
"UserId" column is of Number data type.
I can set string value but I can not set int value.
I have searched a lot but I could not find the solution.
Please help me to solve this issue.
You may get the user_id as a [PFUser currentUser].objectId] so you may set a query by following:
PFQuery *querypush = [PFQuery queryWithClassName:#"EndCall"];
[querypush whereKey:#"UserId" equalTo:[PFUser currentUser].objectId]];
You have to list the value as an object, which will mean an NSNumber in this case. Your code will look like this:
PFQuery *querypush = [PFQuery queryWithClassName:#"EndCall"];
[querypush whereKey:#"UserId" equalTo:#(userID)];
NSNumber can resolve to a lot of different primitive types, including int, but the Parse server needs the NSNumber type to resolve properly.
I'm using the parse backend to be database of my apps in Xcode (objective-c), but I don't know how to write a multiple condition parse format query. I want to convert the below query to a parse format query in the Xcode.
$msg_record = $this->db->query('SELECT msg, send_id, send_time FROM msg_record
WHERE (send_id=123456789 AND to_id=987654321) OR (send_id=987654321 AND to_id=123456789)
ORDER BY send_time ASC')->result();
Can anybody help me to convert the query? Thanks.
To create an or condition in a Parse query, you will have to make two (or more) subqueries which you merge with the orQueryWithSubqueries.
Be aware that you cannot translate SELECT directly from SQL to Parse.
This is what you are looking for:
PFQuery *query1 = [PFQuery queryWithClassName:#"msg_record"];
[query1 whereKey:#"send_id" equalTo:#"123456789"];
[query1 whereKey:#"to_id" equalTo:#"987654321"];
PFQuery *query2 = [PFQuery queryWithClassName:#"msg_record"];
[query2 whereKey:#"send_id" equalTo:#"987654321"];
[query2 whereKey:#"to_id" equalTo:#"123456789"];
PFQuery *mainQuery = [PFQuery orQueryWithSubqueries:#[query1,query2]];
[mainQuery orderByAscending:#"send_time"];
I have a table called "match". this table has several columns of type string.
I'm trying to create a query that queries for the string value "undefined" in column "player2". That works without problem. When I query for just that I get the results.
But I want to add a constraint so that it excludes the match objects where the current user's facebook id matches that of column "player1". I've stored the id in the PFUser object so i can easily retrieve it.
I'm creating a random player matching system where the query checks for an open spot in the match table's "player2" column. if it's "undefined" I know that there is an open slot in the match and the player can join it.
However it needs to exclude the matches the current player previously started himself. Otherwise it would join a match the current player started himself.
//check for objects that match the string "undefined"
PFQuery *query1 = [PFQuery queryWithClassName:#"match"];
[query1 whereKey:#"player2" containsString:#"UNDEFINED"];
//AND constraint to exclude the match of query1 if the current user id matches
PFQuery *query2 = [PFQuery queryWithClassName:#"match"];
[query2 whereKey:#"player1" notEqualTo:[[PFUser currentUser] objectForKey#"fbId"]];
PFQuery *mainQuery = [PFQuery orQueryWithSubqueries:#[query1,query2]];
Your player1 and player2 cols are strings containing user ids. I think this is going to cause other problems, but you can still do the query you're attempting as follows...
NSString *userId = [PFUser currentUser].objectId;
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(player1 = 'UNDEFINED') AND (player2 != %#)", userId];
PFQuery *query = [PFQuery queryWithClassName:#"match" predicate:predicate];
EDIT - I changed fbId above to be the user's object id. That's what you're probably storing in the player string col. It's this kind of confusion that makes storing strings (instead of pointers) a problem for the design.
I'm using Parse within my app. i'm trying to do a query with a limit of 10 objects ordered by descending order of the createdAt property plus I'm using the nearGeopoint:WithinKilometers function. When i run the query, the receive the oldest objects in the database. when i remove the limit of number of objects to be returned, the query returns the latest objects from the newest to oldest (as expected). does the limit do that? isn't it a bug? here is the code, please let me know if there is anything wrong:
PFQuery *postsQuery = [PFQuery queryWithClassName:#"user_p"];
if (city)
{
[postsQuery whereKey:#"geoPoint" nearGeoPoint:[PFGeoPoint geoPointWithLatitude:[city[#"latitude"] doubleValue] longitude:[city[#"longitude"] doubleValue]] withinKilometers:[[[NSUserDefaults standardUserDefaults] valueForKey:#"distanceCovered"] doubleValue]];
}
[postsQuery orderByDescending:#"createdAt"];
[postsQuery includeKey:#"user_id"];
[postsQuery whereKeyExists:#"user_id"];
[postsQuery setLimit:10];
i'm using Parse.com I wanted to ask you something if you can ... How do I delete from the list the total number of users CurrentUser? I would avoid that the report will also be made with oneself: D Thank you all
Perfect Guys .. then I found the method ... In layman's terms to exclude the PFUser Current User from a query to show only the App users but not the CurrentUser is just the use of notEqualTo in the query but it should be associated with the string that we want to remove from the query
I hope this can help other people :)
Do not pay too much attention to the constants: D
PFQuery * query = [self.RelazioniUtenti query];
[query whereKey: FF_USER_NOMECOGNOME notEqualTo: [[PFUser currentUser] objectForKey: FF_USER_NOMECOGNOME]];
[query orderByAscending: FF_USER_NOMECOGNOME];
[query findObjectsInBackgroundWithBlock: ^ (NSArray * objects, NSError * error) {