Start a new topic
Answered

Why is my data not saved to my backend?

Dear users, 


I have been trying to connect my app to my backend on Kinvey, but i am not getting it to work. The backend is connected (i can see the API calls). I have an app where i want to save the data of someones: name, email, number of seats and the date and time. Below you can see my code. Does anyone know why it is not saving to Kinvey?



import UIKit


class ReservationViewController: UIViewController {

   

    @IBOutlet weak var userName: UITextField!

    @IBOutlet weak var userEmail: UITextField!

    @IBOutlet weak var userSeat: UITextField!

    @IBOutlet weak var userDateTime: UIDatePicker!

   

    class Booking : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable

        var entityId: String? //Kinvey entity _id

        var name: String?

        var email: String?

        var seats: String?

        var date: NSDate?

       

    }

   

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {

        return [

            "entityId" : KCSEntityKeyId, //the required _id field

            "name" : "name",

            "email" : "email",

            "seats" : "seats",

            "date" : "date",

        ]

    }

   

   

    @IBAction func sendReservation(sender: AnyObject) {

 // STORE DATA

        let store = KCSAppdataStore.storeWithOptions([

            KCSStoreKeyCollectionName : "userReservation",

            KCSStoreKeyCollectionTemplateClass : Booking.self

            ])

        let reservation = Booking()

        reservation.name = userName.text

        reservation.email = userEmail.text

        reservation.seats = userSeat.text

        reservation.date = NSDate(timeIntervalSince1970: 1352149171) //sample date

        store.saveObject(

            reservation,

            withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in

                if errorOrNil != nil {

                    //save failed

                    NSLog("Save failed, with error: %@", errorOrNil.localizedFailureReason!)

                } else {

 //save was successful

                    NSLog("Successfully saved event (id='%@').", (objectsOrNil[0] as! NSObject).kinveyObjectId())

                }

            },

            withProgressBlock: nil

        )

    }

   

    override func viewDidLoad() {

        super.viewDidLoad()


 // Do any additional setup after loading the view.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

 // Dispose of any resources that can be recreated.

    }

   


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}



Best Answer

Edward,


Yes, you are right and thanks for catching it - hostToKinveyPropertyMapping() should be a function of class Booking. I missed on the closing brace.


Thanks,

Pranav

Kinvey Support


Kevin,

Your code looks good to me. What error do you see when you call the saveObject()?

Thanks,
Pranav
Kinvey Support

 

Pranav, isn't the problem that Booking is a nested class inside of ReservationViewController? hostToKinveyPropertyMapping is currently implemented as a method of ReservationViewController, and not a method of Booking, so when KinveyKit tries checks your Booking object, it doesn't have the method.


I think it would be better if you create a new file - just to keep things clear - and move your Booking class to the new file, then implement hostToKinveyPropertyMapping as part of the Booking class. In fact, another person named Kevin posted the exact same question on stackoverflow.com, which I answered, and never received an upvote for! :) Here was my answer: http://stackoverflow.com/a/36757534/3003312


Hope that helps.

Answer

Edward,


Yes, you are right and thanks for catching it - hostToKinveyPropertyMapping() should be a function of class Booking. I missed on the closing brace.


Thanks,

Pranav

Kinvey Support

Login or Signup to post a comment