let dataStore = backendless.data.of(Alert.self)
dataStore.save(entity: currentAlert, responseHandler: { (savedAlert) in
print("Alert has been saved: \(savedAlert)")
}) { (fault) in
print("Server reported error: \(fault)")
}
print("should have saved alert")
The object currentAlert should be saved to backendless data table. The response handler should return the object savedAlert from which the objectId can be retrieved.
Actual Behavior
The above code successfully saves the data object in backendless table. The response handler does not return any value. There is also no error returned.
I have had a look through the SDK code to try and see what is happening. The function save in DataStoreFactory.swift
public func save(entity: Any, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!) {
let entityDictionary = persistenceServiceUtils.entityToDictionary(entity: entity)
let wrappedBlock: ([String: Any]) -> () = { responseDictionary in
if let resultEntity = self.persistenceServiceUtils.dictionaryToEntity(dictionary: responseDictionary, className: self.persistenceServiceUtils.getClassName(entity: self.entityClass)) {
responseHandler(resultEntity)
print("did get resultEntity")
}
}
persistenceServiceUtils.save(entity: entityDictionary, responseHandler: wrappedBlock, errorHandler: errorHandler)
}
I never receive the message “did get resultEntity”. In addition, if I change the if let resultEntity to let resultEntity I receive a response in the responseHandler but it is nil
Tried to save a new object and everything works fine - I’ve took your class
@objcMembers class Alert: NSObject, NSCoding
and your code
let dataStore = backendless.data.of(Alert.self)
dataStore.save(entity: currentAlert, responseHandler: { (savedAlert) in
print("Alert has been saved: \(savedAlert)")
}) { (fault) in
print("Server reported error: \(fault)")
}
print("should have saved alert")
To create a test case I downloaded the ‘project template’ from the Backendless console. I then edited the following function:
func saveTestObject() {
@objcMembers class MyAlert: NSObject {
var objectId: String?
var title: String?
var count = 42
}
let newAlert = MyAlert()
newAlert.title = "Test Title"
Backendless.shared.data.of(MyAlert.self)
.save(entity: newAlert, responseHandler: {
print("Did saved new alert: \($0)")
}, errorHandler: {
print("Error alert: \($0)")
})
dataStore = Backendless.shared.data.ofTable("TestTable")
testObject = ["foo" : "Hello World"]
dataStore?.save(entity: testObject, responseHandler: { savedTestObject in
DispatchQueue.main.async {
self.objectSavedLabel.text = "Object has been saved in the real-time database"
self.liveUpdateObjectPropertyLabel.text = "Live update object property"
self.propertyLabel.text = savedTestObject["foo"] as? String
}
self.testObject = savedTestObject
let eventHandler = self.dataStore?.rt
if let savedObjectId = savedTestObject["objectId"] as? String {
let whereClause = String(format: "objectId = '%@'", savedObjectId)
print("objectID: \(savedObjectId)")
let _ = eventHandler?.addUpdateListener(whereClause: whereClause, responseHandler: { updatedTestObject in
if updatedTestObject["foo"] != nil {
self.propertyLabel.text = updatedTestObject["foo"] as? String
}
}, errorHandler: { fault in
self.showErrorAlert(fault)
})
}
}, errorHandler: { fault in
self.showErrorAlert(fault)
})
}
In this case I successful receive the savedObjectId from the data saved to TestTable. I am still not receiving the response for the call to save to MyAlert table
I have modified my code to convert from the Alert class to a [String : Any] data structure. See updated code below:
let dataStore = backendless.data.ofTable("Alert")
var alertDataStructure: [String : Any]!
alertDataStructure = ["deviceId":deviceID,"coin":alert.coin,"criteria":alert.criteria,"value":alert.value,"dateCreated":alert.dateCreated,"enabled":alert.enabled,"price":alert.price,"volAUD":volAUD,"volUSD":volUSD]
dataStore.save(entity: alertDataStructure, responseHandler: { (savedAlert) in
if let savedObjectId = savedAlert["objectId"] as? String {
print("alert savedObjectId = \(savedObjectId)")
self.alerts[tableRow].objectId = savedObjectId
self.saveAlerts()
}
}) { (fault) in
print("Server alert reported error: \(fault)")
}
print("should have saved alert")
By calling let dataStore = backendless.data.ofTable("Alert") rather than let dataStore = backendless.data.of(Alert.self) I am now successfully receiving the ObjectId in the response handler.
Not sure why you cannot reproduce the issue, but it seems that I can use this method as a work around for now.
Can you find PersistenceServiceUtils.swift file in SDK, set breakpoint at the 522th line and check value of resultEntityTypeName variable after calling save()?