Hello guys.
I’m using the async method from your documentation to save an object on Backendless.
The problem is that not every field of my object is getting saved.
For example, these properties are not nil before i save the object:
(lldb) po studyToSave.configLane
▿ Optional<Int>
- some : 2
(lldb) po studyToSave.configTube
▿ Optional<Int>
- some : 2
(lldb) po studyToSave.spacing
▿ Optional<Double>
- some : 20.0
(lldb) po studyToSave.daysBetween
▿ Optional<Double>
- some : 3.0
But they are nil on the async call response object
(lldb) po obj.configLane
nil
(lldb) po obj.configTube
nil
(lldb) po obj.spacing
nil
(lldb) po obj.daysBetween
nil
Here’s my code:
let backendless = Backendless.sharedInstance()
let studyToSave: Study = study!
let dataStore = backendless?.data.of(Study.ofClass())
// save object asynchronously
dataStore?.save(studyToSave, response: { (response) in
let obj = response as! Study
print("Study has been saved: \(obj.studyId ?? "no study id")")
}, error: { (error) in
if let theError = error {
print("save study error " + theError.message) }
self.navigationController?.popToRootViewController(animated: true)
})
Thanks in advance!
Hey Vlad,
Can you please publish the source code of your Study class?
Hello Vlad,
Do you have Backendless initialization?
What version are you using 3 or 4?
Please provide us your APP_ID and the source code of your Study class, so we could check the issue.
Regards, Olga
Hello Sergey.
Hello Olga.
I’m using version 3.
let APP_ID = "AB797EB8-18BF-C13D-FF1B-86001DC11000"
var backendless = Backendless.sharedInstance()
backendless?.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
Here’s the source code of the Study class:
struct StudyState {
static let New = "New"
static let InProgress = "In Progress"
static let DataRetrieved = "Data Retrieved"
}
struct StudyStatus {
static let Pending = "Pending start: "
static let Active = "Active: "
static let Ended = "Study ended, pending retrieval"
static let EndedAndRetrieved = "Study ended, data retrieved, pending upload"
static let Uploaded = "Study uploaded"
static let IncompleteInfo = "Not enough information"
}
struct StudyCodingKeys {
static let StudyName = "studyName"
static let Mph = "mph"
static let DwellTime = "dwellTime"
static let FirmwareVersion = "firmwareVersion"
static let ConfigTube = "configTube"
static let ConfigLane = "configLane"
static let DaysBetween = "daysBetween"
static let DeviceId = "deviceId"
static let Direction = "direction"
static let EditTitle = "editTitle"
static let StartDate = "startDate"
static let EndDate = "endDate"
static let IsHidden = "isHidden"
static let IsPrivate = "isPrivate"
static let Latitude = "latitude"
static let Longitude = "longitude"
static let Location = "location"
static let Measurement = "measurement"
static let Notes = "notes"
static let OppDirection = "oppDirection"
static let StudyId = "studyId"
static let Status = "status"
static let StudyState = "studyState"
static let Spacing = "spacing"
static let Uploaded = "uploaded"
static let ObjectId = "objectId"
static let OwnerId = "ownerId"
}
class Study: NSObject, NSCoding {
var mph: Double?
var studyName: String?
var dwellTime: Double?
var firmwareVersion: Double?
var configTube: Int?
var configLane: Int?
var daysBetween: Double?
var deviceId: String?
var direction: String?
var editTitle: Bool?
var endDate: Date?
var startDate: Date?
var isHidden: Bool?
var isPrivate: Bool?
var latitude: Double?
var longitude: Double?
var location: String?
var measurement: String?
var notes: String?
var oppDirection: String?
var studyId: String?
var spacing: Double?
var status: Int?
var studyState: String?
var uploaded: Bool?
var objectId: String?
var ownerId: String?
///
var statusString: String?
override init() {
super.init()
}
init (studyName: String) {
self.studyName = studyName
}
required init?(coder aDecoder: NSCoder) {
if let nameObj = aDecoder.decodeObject(forKey: StudyCodingKeys.StudyName) as? String {
self.studyName = nameObj
}
if let mphObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Mph) as? Double {
self.mph = mphObj
}
if let dwellTimeObj = aDecoder.decodeObject(forKey: StudyCodingKeys.DwellTime) as? Double {
self.dwellTime = dwellTimeObj
}
if let firmwareVersionObj = aDecoder.decodeObject(forKey: StudyCodingKeys.FirmwareVersion) as? Double {
self.firmwareVersion = firmwareVersionObj
}
if let tubeObj = aDecoder.decodeObject(forKey: StudyCodingKeys.ConfigTube) as? Int {
self.configTube = tubeObj
}
if let laneObj = aDecoder.decodeObject(forKey: StudyCodingKeys.ConfigLane) as? Int {
self.configLane = laneObj
}
if let daysBetweenObj = aDecoder.decodeObject(forKey: StudyCodingKeys.DaysBetween) as? Double {
self.daysBetween = daysBetweenObj
}
if let deviceIdObj = aDecoder.decodeObject(forKey: StudyCodingKeys.DeviceId) as? String {
self.deviceId = deviceIdObj
}
if let directionObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Direction) as? String {
self.direction = directionObj
}
if let startDateObj = aDecoder.decodeObject(forKey: StudyCodingKeys.StartDate) as? Date {
self.startDate = startDateObj
}
if let endDateObj = aDecoder.decodeObject(forKey: StudyCodingKeys.EndDate) as? Date {
self.endDate = endDateObj
}
if let latitudeObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Latitude) as? Double {
self.latitude = latitudeObj
}
if let longitudeObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Longitude) as? Double {
self.longitude = longitudeObj
}
if let locationObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Location) as? String {
self.location = locationObj
}
if let notesObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Notes) as? String {
self.notes = notesObj
}
if let studyIdObj = aDecoder.decodeObject(forKey: StudyCodingKeys.StudyId) as? String {
self.studyId = studyIdObj
}
if let spacingObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Spacing) as? Double {
self.spacing = spacingObj
}
if let studyStateObj = aDecoder.decodeObject(forKey: StudyCodingKeys.StudyState) as? String {
self.studyState = studyStateObj
}
if let measurementObj = aDecoder.decodeObject(forKey: StudyCodingKeys.Measurement) as? String {
self.measurement = measurementObj;
}
}
func encode(with aCoder: NSCoder) {
aCoder.encode(studyName, forKey: StudyCodingKeys.StudyName)
aCoder.encode(mph, forKey: StudyCodingKeys.Mph)
aCoder.encode(dwellTime, forKey: StudyCodingKeys.DwellTime)
aCoder.encode(firmwareVersion, forKey:StudyCodingKeys.FirmwareVersion)
aCoder.encode(configTube, forKey: StudyCodingKeys.ConfigTube)
aCoder.encode(configLane, forKey: StudyCodingKeys.ConfigLane)
aCoder.encode(daysBetween, forKey: StudyCodingKeys.DaysBetween)
aCoder.encode(deviceId, forKey: StudyCodingKeys.DeviceId)
aCoder.encode(direction, forKey: StudyCodingKeys.Direction)
aCoder.encode(startDate, forKey: StudyCodingKeys.StartDate)
aCoder.encode(endDate, forKey: StudyCodingKeys.EndDate)
aCoder.encode(latitude, forKey: StudyCodingKeys.Latitude)
aCoder.encode(longitude, forKey: StudyCodingKeys.Longitude)
aCoder.encode(location, forKey: StudyCodingKeys.Location)
aCoder.encode(notes, forKey: StudyCodingKeys.Notes)
aCoder.encode(studyId, forKey: StudyCodingKeys.StudyId)
aCoder.encode(spacing, forKey: StudyCodingKeys.Spacing)
aCoder.encode(studyState, forKey: StudyCodingKeys.StudyState)
aCoder.encode(measurement, forKey: StudyCodingKeys.Measurement)
}
}
I have created an internal task (ID BKNDLSS-15166) to investigate the issue. We shall notify you as soon as we have any news. Thanks in advance for your patience!
Ok.Thanks a lot for your quick responses, guys!
Hello, Vlad
Thanks for sharing your Study class code.
For Swift data objects the Bool, Int, Double and Float types must not be declared as optional. And every Int, Double and Bool property in your Study class is Optional. Please change these properties to nonOptional and everything will work fine.
var mph: Double = 0.0
var studyName: String?
var dwellTime: Double = 0.0
var firmwareVersion: Double = 0.0
var configTube: Int = 0
var configLane: Int = 0
var daysBetween: Double = 0.0
var deviceId: String?
var direction: String?
var editTitle: Bool = false
var endDate: Date?
var startDate: Date?
var isHidden: Bool = false
var isPrivate: Bool = false
var latitude: Double = 0.0
var longitude: Double = 0.0
var location: String?
var measurement: String?
var notes: String?
var oppDirection: String?
var studyId: String?
var spacing: Double = 0.0
var status: Int = 0
var studyState: String?
var uploaded: Bool = false
var objectId: String?
var ownerId: String?
Regards, Olga
Ok. Thanks a lot for your help, Olga
Have a good day.
Cheers!