Get User class user details by using pointer in other class - ios

In some condition i need current user and other user.I am getting current user data fine, but i dont know how to get other user data.my code is
PFUser *user1 = [PFUser currentUser];
PFObject *user2 = [PFObject objectWithoutDataWithClassName:#"_User" objectId:Objid];
my output for user1 is
<PFUser: 0x7fd3427923f0, objectId: a1P2ZZf46E, localId: (null)> {
email = "xxxx#gmail.com";
fullname = xxxx;
picture = "<PFFile: 0x7fd342791950>";
thumbnail = "<PFFile: 0x7fd342791ee0>";
username = "xxxx#gmail.com";
}
but for user2 is
<PFUser: 0x7fd3448b8bd0, objectId: XluNx9rZdV, localId: (null)> {
}

Because you are using objectWithoutDataWithClassName which will return you back PFObject with objectId only. So, if you want to get the full object by its objectId, you can use + (PF_NULLABLE PFObject *)getObjectOfClass:(NSString *)objectClass objectId:(NSString *)objectId to get back the object like so:
[PFQuery getObjectOfClass:#"_User" objectId:Objid];

You may try something like this:
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" equalTo:Objid];
NSArray *user2 = [query findObjects];
PFuserProfile = [user2 firstObject];

Related

objectForKey not working for pointer

In the docs https://www.parse.com/docs/ios/guide#relations-using-pointers
it says that in the example provided you can find the User who created the game
// say we have a Game object
PFObject *game = ...
// getting the user who created the Game
PFUser *createdBy = [game objectForKey:#"createdBy"];
But when I use the exact syntax since I want to populate the pointer in my "user" column
PFUser *user = [PFUser currentUser];
NSString *username = user.username;
// Inside my queryForTable method so PFObject is returned in
// tableView:cellForRowAtIndexPath:object
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"fromUser = %#", user];
PFQuery *query = [PFQuery queryWithClassName:#"Activities" predicate:predicate];
// Inside tableView:cellForRowAtIndexPath:object
PFUser *createdBy = [object objectForKey:#"user"];
NSLog(#"User to user ---%#", createdBy);
But All I get back is
User to user ---<PFUser: 0x7ff9024da860, objectId: aOisP569e3, localId: (null)> {
// Nothing here
}
If I'm understanding correctly, am I also supposed to get back username, email etc in my user object?
---UPDATE 1---
looking at the Anypic app provided by parse it should return something like this
<PFUser: 0x7f7ff9fafc00, objectId: LfADtx1K2J, localId: (null)> {
// Stuff appears here
displayName = "poopiu";
facebookId = 130941340571204;
profilePictureMedium = "<PFFile: 0x7f7ff9faf500>";
profilePictureSmall = "<PFFile: 0x7f7ff9faf7f0>";
username = I34MBM3WYSB5tjWIIvUvhH5fq;
}
but mine is empty even though I have a column called username that isn't undefined so I should get that inside my PFUser object
--UPDATE 2--
Here's what I get back from logging object like so...
NSLog(#"Object---%#", object);
<Activities: 0x7fbbebf42d20, objectId: rDwYI5Inuk, localId: (null)> {
user = "<PFUser: 0x7fbbee1367e0, objectId: SFL0kVZ17x>";
status = 0;
>
Add the following method call after you instantiated your query.
[query includeKey: "user"];
By default, queries do not grab information past the immediate object that was queried.
Are you sure your user isn't nil? According to the docs you must be logged in for currentUser to return a user object.
[createdBy fetch];
NSLog(#"User to user ---%#", createdBy);
Try this hope this will help

iOS parse issues when retrieving pointer to PFUser

I have a Follower class where stores all following/follower relations of my app. However, when I was trying to retrieve the pointer of each PFUser, I can't get the attributes of each PFUser. Here is my code
PFQuery *query = [PFQuery queryWithClassName:#"Follower"];
[query whereKey:#"from" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
for(PFObject *o in objects)
{
PFUser *otherUser = (PFUser *)[o objectForKey:#"to"];
NSLog(#"%#",otherUser);
NSLog(#"%#",otherUser.username);
NSString *nickName = otherUser[#"nickName"];
cell.friendNameLabel.text = nickName;
NSLog(#"%#", otherUser[#"nickName"]);
//cell.friendUsrnameLabel.text = otherUser.username;
PFFile *imgFile = otherUser[#"profilePhoto"];
profileView.file = imgFile;
[profileView loadInBackground];
}
}
else
{
NSLog(#"error");
}
}];
when I tried to print every user, here is what i got from the console:
<PFUser: 0x7fc102ec9f70, objectId: GiOIGiNHjK, localId: (null)> {
}
so it didn't find the attributes of each user. Anyone knows the solution? Thanks.
Try to add this line before executing the query.
[query includeKey:#"to"];

Allow only 2 users to read an object from a public class

I'm building a private chat between 2 users. Currently I've got the "Chat" class open to everyone (read/write) and there is where all messages (objects) go.
I was thinking about adding objects with permission to read only between two users so only they can see what they chat.
I'm grabbing the messages using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
PFQuery *fquery = [PFUser query]; // query to get the chat partener
[fquery whereKey:#"objectId" equalTo:[[NSUserDefaults standardUserDefaults] stringForKey:#"friendid"]];
PFUser *friend = (PFUser *)[fquery getFirstObject]; // got it!
PFQuery *query = [PFQuery queryWithClassName:#"Chat"]; // new query for grabbing messages
[query whereKey:PF_CHAT_ROOM equalTo:chatroom]; // #"Chat" = #"Chat"
if (message_last != nil) [query whereKey:PF_CHAT_CREATEDAT greaterThan:message_last.date];
[query includeKey:PF_CHAT_USER]; // current user
[query includeKey:[NSString stringWithFormat:#"%#", friend]]; // its friend/partener
[query orderByAscending:PF_CHAT_CREATEDAT]; // sort by date
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
// adding the messages to an array;
}];
For writing messages to parse:
PFObject *object = [PFObject objectWithClassName:#"Chat"]; // class name
object[PF_CHAT_ROOM] = chatroom;
object[PF_CHAT_USER] = [PFUser currentUser];
object[PF_CHAT_TEXT] = text;
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" equalTo:[[NSUserDefaults standardUserDefaults] stringForKey:#"friendid"]];
PFUser *friend = (PFUser *)[query getFirstObject]; // query to get the chat partener
PFACL *roleACL = [PFACL ACL];
[roleACL setReadAccess:YES forUser:[PFUser currentUser]];
[roleACL setReadAccess:YES forUser:friend]; // setting read permission for those guys
object.ACL = roleACL;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
....
}];
Is this the right way to go?
I've implemented a similar app taking the same approach which seemed to work. But I think you've over complicated for what it needs to be.
You need a Chat class which you seem to have. This may be something similar to:
Chat
Object ID, Message, Author, Date, Date Seen, ACL, etc....
(there's many good schemas out there just google it)
Then when you create a new chat message to a friend you simply just create a new PFObject setting the other user's ACL permission to like you've done correctly.
The user can read his messages by calling
PFQuery *query = [PFQuery queryWithClassName:#"Chat"];
The response of the query will only be the chats that the user has read permission to, not everyones. Therefore there's no need to specify any further query parameters unless you're looking for unread messages, etc..

Parse.com - How to search/retrieve secured object for current uer

As per parse API documentation, "Security For Other Objects".
Create a private note that can only be accessed by the current user:
PFObject *privateNote = [PFObject objectWithClassName:#"Note"];
privateNote[#"content"] = #"This note is private!";
privateNote.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
[privateNote saveInBackground];
Link: https://www.parse.com/docs/ios_guide#users-acls/iOS
This note will then only be accessible to the current user, although it will be accessible to any device where that user is signed in.
Question: How to retrieve all my private notes ? Unable to find out in documentation.
You have one add more column in your note class named "user" it creates user pointer in your table.
PFObject *privateNote = [PFObject objectWithClassName:#"Note"];
privateNote[#"content"] = #"This note is private!";
privateNote[#"user"] = [PFUser currentUser];
privateNote.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
[privateNote saveInBackground];
And when you want to retrieve all notes which are related to that user then you can use following code
PFQuery *query = [PFQuery queryWithClassName:#"Note"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
NSArray *usersNote = [query findObjects];
NSLog(#"%#",usersNote);
in above Array u can get records.
Hope this will help to you.
Actually I've got another simple solution:
NO need to create extra column. You can simply use this:
PFObject *privateNote = [PFObject objectWithClassName:#"Note"];
privateNote[#"content"] = #"This note is private!";
privateNote.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
[privateNote saveInBackground];
PFQuery *query = [PFQuery queryWithClassName:#"Note"];
NSArray *usersNote = [query findObjects];
NSLog(#"%#",usersNote);
It will automatically retrieve private note.

Filter NSMutableArray Containing A List of Messages

I am trying to filter and sort an NSMutableArray containing a list of Chat Messages. I am trying to get the Last Message of a conversation between 2 users. For example if user1 has 2 different conversations with 2 separate users, I want to get the last message of each of those conversations. I am using Parse.com as backend and this how I saved & retrieved the messages.
Saving the Messages
PFUser *user = [[PFUser currentUser] objectForKey:#"displayName"];
PFObject *newMessage = [PFObject objectWithClassName:#"Messages"];
[newMessage setObject:messageStr forKey:#"body"];
[newMessage setObject:self.mySelectedUser forKey:#"toUser"];
[newMessage setObject:user forKey:#"fromUser"];
[newMessage saveInBackground];
myMessageField.text = #"";
[self getTheNewMessages];
[self.myTView reloadData];
Retrieving The Messages
-(void)GetmyNewMessages
{
PFQuery *query1 = [PFQuery queryWithClassName:#"Messages"];
[query1 whereKey:#"toUser" equalTo:[[PFUser currentUser] objectForKey:#"displayName"]];
[query1 whereKeyExists:#"date"];
PFQuery *query2 = [PFQuery queryWithClassName:#"Messages"];
[query2 whereKey:#"fromUser" equalTo:[[PFUser currentUser] objectForKey:#"displayName"]];
[query2 whereKeyExists:#"date"];
PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:query1, query2, nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"Successfully retrieved %d chats.", objects.count);
[premessagesList removeAllObjects];
[premessagesList addObject:objects];
} else {
//Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
NSLog of the NSMutableArray (premessageList)
( "<Messages:aS8tVIlsHc:(null)> {\n
body = hi;\n fromUser = cnn;\n toUser =FNC;\n
date = \"2012-09-19 02:40:29 +0000\";\n}",
"<Messages:CPCa6VBmf7:(null)> {\n
body = hello;\n fromUser = FNC;\n toUser =cnn;\n
date = \"2012-09-20 05:06:05 +0000\";\n}",
"<Messages:Jz1cILt18Y:(null)> {\n
body = whatsgood;\n fromUser = sleepy;\n toUser =cnn;\n
date =\"2012-09-20 05:06:05 +0000\";\n}",
"<Messages:lXretmE1uK:(null)> {\n
body = lol;\n fromUser = cnn;\n toUser =sleepy;\n
date =\"2012-09-20 05:13:16 +0000\";\n}",
I tried to use NSSortDescriptor and NSPredicate but I got an empty tableview.
In this type of problem, I would use some variables and fast iteration to slog through the info and get what you need:
id lastChat = ni;
... temps
for(OneChat in allChats) {
do tests and set variables
}
Now I know the last chat.
PS: I decided not to post this, but when I saw after a long time no one had responded felt it was better than nothing. This is how I would solve this.
You could limit the results and sort through the PFQuery itself like this:
[query1 orderByDescending:#"createdAt"];
query1.limit = 1;
[query2 orderByDescending:#"createdAt"];
query2.limit = 1;

Resources