1

Why does my data is not send to the Kinvey backend? My app keeps crashing and I get the SIGABRT error. I have tried changing the String to NSString but that didn't help. For some reason, my dashboard in Kinvey does show API Calls and Data storage but it's not storing anything (I am a beginner).

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!

    @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
        )
    }

    class Booking : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String? //Kinvey entity _id
        var name: NSString?
        var email: NSString?
        var seats: NSString?
        var date: NSDate?

    }

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "name" : "name",
            "email" : "email",
            "seats" : "seats",
            "date" : "date",
        ]
    }


    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.
    }
}
Caleb
  • 5,548
  • 3
  • 25
  • 32
Kevin
  • 39
  • 6

1 Answers1

1

If that is actually your code and not just a mistake you made when copy-pasting, the problem is that your Booking class is currently a nested class in your ReservationViewController, and you implemented hostToKinveyPropertyMapping as a method of ReservationViewController.

Instead, create a new file for your Kinvey class:

class Booking: NSObject {
    var entityId: String? //Kinvey entity _id
    var name: NSString?
    var email: NSString?
    var seats: NSString?
    var date: NSDate?

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId, //the required _id field
            "name": "name",
            "email": "email",
            "seats": "seats",
            "date": "date"  //You also had an extra comma after the last item of this dictionary.
        ]
    }
}

And delete that code from your view controller. Also, make sure you safely unwrap all optional values, and confirm that your collection really is named "userReservation".

ConfusedByCode
  • 1,137
  • 8
  • 27
  • sorry for not replying sooner i have been on vacation for the past 3 weeks I will test this as soon as i come back this week!, I am still a little confused by what you mean by creating a new file? – Kevin Apr 27 '16 at 19:21
  • No problem, I was just joking! I meant you should add a new Swift file to your XCode project, and put the code for the Booking class there, just to clearly separate it from your ReservationViewController. I think they should be two completely separate classes. – ConfusedByCode Apr 27 '16 at 19:39
  • How do I call upon the code if it is in a new class and not connected to the reservation view controller? Im sorry kinda new to this... – Kevin May 03 '16 at 08:55
  • You do it the same way you're doing it now: `let booking = Booking()`, and then you can manipulate your booking object as you wish. It's common practice when working in an IDE like Xcode to create a separate file for each class. – ConfusedByCode May 07 '16 at 00:28