Search code examples
swiftparse-platform

How to get createdAt property when saving a new object using Parse SDK?


I am creating a new object and for some reason I need to get the the full saved object including the createdAt and updatedAt properties.

in this code, I am saving the new object.

        let msg = PFObject(className: "ChatMessage")
        msg["User"] = user
        msg["Message"] = message
        msg["Topic"] = topic
        //messageList.append(message)
        msg.saveInBackground { (success, error) in
            if error == nil {
                //msg.createdAt is nil
            }
        }

How can I get the full saved object without making another query to fetch it?

Edit

this is the vars values

I can see that the "createdAt" value is available inside "_pfinternal_state" object but not in "_estimatedData.

enter image description here

Is there any way I can access _pfinternal_state._createdAt ?

Thanks


Solution

  • I spent a lot of time finding a solution for this small problem.

    You can simply. get createdAt and updatedAt values by calling "value(forKey:..." function and cast the returned value to NSDate, as the next code:

            let msg = PFObject(className: "ChatMessage")
            msg["User"] = user
            msg["Message"] = message
            msg["Topic"] = topic
            //messageList.append(message)
            msg.saveInBackground { (success, error) in
                if error == nil {
                    //msg.createdAt is nil
                    let createdAt = msg.value(forKey: "createdAt") as! NSDate 
                    // this will return the NSDate object. 
                    //   You must cast it to NSDate not Date
    
                }
            }