1

I have Messages class table and inside the table contains "messageTextColumn" column and "user" column.

This "user" column, I created it with type pointer and the target class is _User which is parse main class.

I want to store both user's saved messages and the userId so that I can query it and show it in that user's message tableview cell which will show the list of every messages that the user had saved in my app.

This is my code:

 @IBAction func addMessage(sender: AnyObject) {
    var newMessage = addMessageText.text

    let message = PFObject(className: "Messages")
    var user = PFUser.currentUser()
    message["messageTextColumn"] = newMessage
    message["user"] = user

    message.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
        if (success) {
            println("added to Message Class")
           println(user)
            message.saveInBackground()
        } else {
            // Error saving message
        }
    }
}

The problem is when I run this, the log shows:

[Error]: can't add a non-pointer to a relation (Code: 111, Version: 1.7.4)

and the message was not saved on Parse at all but when I deleted this line of code:

message["user"] = user

I was able to save the message to Parse Messages class table but not the userID of the user who saved it.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
sin90
  • 171
  • 1
  • 12

2 Answers2

1

In your code you're trying to add an entire user object when you say you only want to add the user's object ID. If you only want to add the user's object ID then you would do the following:

// ...
message["user"] = PFUser.currentUser().objectId

And in your database your user column would be of String type.

If you actually wanted to add the entire user object as a relation to your message object then you would add the user like you did:

// ...
message["user"] = PFUser.currentUser()

And in your database your user column would be of Pointer type to _User.

Aaron
  • 6,466
  • 7
  • 37
  • 75
0

I figured it out! This is a Swift language issue. Write the query in Obj-C and it should work perfectly!

  • Fleshing out the answer with a sample code snippet would have gone a long way towards making this a good answer. –  Mar 23 '16 at 21:12