Object not saving to database -- back4app issue?

39 views
Skip to first unread message

Max Tannone

unread,
Feb 11, 2017, 1:13:13 PM2/11/17
to back{4}app
Trying to save an object to my database, cannot figure out where I am going wrong. 


message["fromUser"] = PFUser.current()


message["toUser"] = theRemoteUser


message
["messageText"] = theMessage

message.saveInBackground(block: { (success, error) in

               

               if success {

                   

                   print("success!")

 

                   //the object has been saved

                   

               }

               

               else {

                   

                   print("error!")

                   

                   print(error)


                  

                   

               }


            })




Success is never reached, yet error prints out "nil".

Yet, strangely, one time out of dozens, the message actually DID save to the database. No changes in my code though. I wonder if something on the back4app end has to be reset or something using my app ID?

Thanks!

Max Tannone

unread,
Feb 11, 2017, 1:13:56 PM2/11/17
to back{4}app
wanted to add this at the top of the code:

var message = PFObject(className: "Message")

casag...@back4app.com

unread,
Feb 24, 2017, 12:33:03 PM2/24/17
to back{4}app
Hello Max,

It seems since that error is showing up, maybe the "message" is not receiving anything. Possibly you're not getting the class correctly so the object stays empty.

Best!

Max Tannone

unread,
Feb 24, 2017, 12:36:22 PM2/24/17
to back{4}app
I fixed the issue. I was trying to store user pointers incorrectly. I thought I could just them equal to a user object, but I had to manually create the pointer in code, and assign that instead.

message["toUser"] = PFObject(withoutDataWithClassName: "_User", objectId: theRemoteUser?.objectId)


Actually, you can set a user pointer column directly equal to a user, but only for PFUser.current()

var message = PFObject(className:"Message")

            

            message["fromUser"] = PFUser.current()

            

            message["toUser"] = PFObject(withoutDataWithClassName: "_User", objectId: theRemoteUser?.objectId)

            

            message["messageText"] = theMessage

casag...@back4app.com

unread,
Feb 24, 2017, 12:37:17 PM2/24/17
to back{4}app
I see,

Thanks for the feedback then!

Best!
Reply all
Reply to author
Forward
0 new messages