为什么我的代码没有将数据保存到Kinvey后端?

时间:2016-04-12 11:39:50

标签: swift data-storage kinvey

我有一个viewController,它有三个文本字段(名称,电子邮件和座位)和日期选择器。下面有一个按钮作为插座连接,以便将预订发送到应该注册的后端。我已经尝试了几件事,但由于某种原因,数据没有保存。我连接到后端因为我可以看到字节中的Api调用和数据存储....你能检查一下我的代码吗?

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    self.sendReservation.addTarget(self, action: "sendBooking:", forControlEvents: .TouchUpInside)

    let reservation = Booking()
    reservation.name = userName.text
    reservation.email = userEmail.text
    reservation.seats = userSeat.text
    reservation.date = NSDate(timeIntervalSince1970: 1352149171) //sample date

          func sendBooking (sender: UIButton) {

        KCSUser.loginWithUsername("admin", password: "leden1234",
            withCompletionBlock: { (user: KCSUser!, errorOrNil: NSError!, result: KCSUserActionResult) -> Void in
                if errorOrNil == nil {

        let store = KCSAppdataStore.storeWithOptions([
            KCSStoreKeyCollectionName : "userReservation",
            KCSStoreKeyCollectionTemplateClass : Booking.self
            ])

                    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
                    ) } else {

                    NSLog("User was not registered: %@", errorOrNil.localizedFailureReason!)

                }

                })
    }
}

}

2 个答案:

答案 0 :(得分:0)

所有Kinvey请求必须使用授权用户,如果用户不存在,则存在自动用户生成工具。

确保您是活跃用户,即您在进行任何Kinvey通话之前已登录。我已尝试过您的代码并且运行正常。尝试以下实施:

int *

有关详细信息,请查看http://devcenter.kinvey.com/ios/guides/users#

如果以上信息无效,请告诉我您在“errorOrNil.localizedFailureReason”中收到的错误。

答案 1 :(得分:0)

尝试使用它与我合作

KCSUser.userWithUsername(Username_user.text!, password: Password.text!, fieldsAndValues: [KCSUserAttributeEmail : Email.text!,
                KCSUserAttributeGivenname : UserName.text! ,KCSUserAttributeSurname : SureName.text!],
 withCompletionBlock: { (user: KCSUser!, errorOrNil: NSError!, result: KCSUserActionResult) -> Void in

 if errorOrNil == nil {

}else{

}

})