Retrieve User Data from "ownerId"

I’m trying to retrieve user data based off the ‘ownerid’ in a post table!

@try {
BackendlessUser *user = [comment valueForKey:@"ownerId"];
NSLog(@"User Photo: %@", user);
if (user) {
NSString *photo = [user getProperty:@"photo"];
NSLog(@"User Photo: %@", photo);
}
else {
NSLog(@"User hasn't been logged");
}
}
@catch (Fault *fault) {
NSLog(@"Server reported an error: %@", fault);
}

Seems to be crashing the app, any idea why?

Hi Haley,

Please clarify, if really property ‘ownerId’ in “comment”’ object has been set with BackendlessUser object?

Because valueForKey: returns untyped object, and
[user getProperty:@“photo”];
can be a cause of crash.

Regards,
Slava

You’re right, it is a backlendlessuser. It is clickable.

Screen Shot 2016-02-11 at 6.29.37 PM.png

This will not work:

BackendlessUser *user = [comment valueForKey:@"ownerId"];

the ownerId column has type STRING (on the backend). On the client side it will not be returned as a BackendlessUser object.

Could you describe (without code, just plain English) what you want to accomplish?

Regards,
Mark

I’m trying to get the photo and name of the user who left a comment. Since the user’s id is stored in ownerid, i’d trying to retrieve the linked information.

Here’s the code I used w/ parse:

PFUser *user = [comment objectForKey:@"writer"];
    [user fetchIfNeeded];
   
    commentPhoto = user[@"image"];
    [commentPhoto getDataInBackground];

Do you know objectId of the user who left the comment?

Yes. It is saved in “ownerId” of the “Comment Table.” The string is a clickable one, which when clicked opens a filtered “Users” Table with only that user’s details.

Screen Shot 2016-02-11 at 6.55.56 PM.png

I am asking if in your client program you know objectId of the use for which you need to load the name and photo.

Also, it would be amazingly helpful if you could provide the following information:

  • where does your app store comments?
  • are they in a separate table?
  • is there a relationship between users and comments?
  • is name and photo stored in the user objects? If not, where?

We’d love to help out, however, with the vacuum of information and cropped images showing very little helpful info, it makes it extremely difficult to write any kind of meaningful query to show you how to get things done.

Mark

I’ve explained it as best I know, sorry for bothering you. The files used are attached.

Sorry, I cannot help with the code. Here’s what I would need so we can help you:

    Please attach screenshots from Backendless console showing the schema for all the tables you’re working with. The schema is located when you click this button for any given table:http://support.backendless.com/public/attachments/a9aa106de2161eeb4cfe52cfa58cda56.png</img>
    A screenshot of the user properties screen from backendless console. The screen is available when you go to Users > User Properties.
Thanks, Mark

ownerid is automatically added to every table, right?! If the user is logged in, the user’s objectid is put in there for each new post / comment added. how do you get all of the user’s information based on the id stored in ownerid? that’s really what i’m asking…

in your own screenshot, when you click the blue links in ownerid, you’d understand what i mean, i think?

ownerid is automatically added to every table, right?! 

Yes.

Are you going to be okay with an example in Swift? My Obj-C is a bit rusty…

Haha, my swift is rustier but give it a shot, I’d tackle it from there! Thanks!

The following assumptions are made for the following code:

    There is a logged in user There is the Comment class which is created by users and ownerId is set for the Comment objects:
let dataQuery = BackendlessDataQuery();
dataQuery.whereClause = "ownerId = '\(backendless.userService.currentUser.objectId)'"
        
// get all the comments created by user
let collection:BackendlessCollection = backendless.data.of(Comment.ofClass()).find(dataQuery)
        
// as an example, get the first comment
let commentObject = collection.getCurrentPage().first as! Comment;