As of April 12th, you must go to Progress SupportLink to create new support cases or to access existing cases. Please, bookmark the SupportLink URL and use the new portal to contact the support team.
Can't save KCSPersistable objects with nil properties
D
Denny Tsai
started a topic
about 7 years ago
From the documentation all the properties are declared with Optional types, but it seems like since the library is using NSJSONSerialization, the properties can't actually have nil values. So does that mean when I'm about the save the object I need to make sure all the properties need to have values? How do I have some properties that are optional?
Right now I'm overriding valueForKey: method to make the property return NSNull if the property is nil. Just wondering if there's a better way to do this or maybe I'm doing something totally wrong and the library should be handling optional properties automatically.
Here's the workaround code that I have currently:
override func valueForKey(key: String) -> AnyObject? {
if let value = super.valueForKey(key) {
return value
} else {
return NSNull()
}
}
Best Answer
P
Pranav J
said
about 7 years ago
Denny,
This seems to be working fine. Here is a sample:
class JobRole : NSObject { //all NSObjects in Kinvey implicitly implement KCSPersistable var entityId: String? //Kinvey entity _id var name: String? var date: NSDate? var fileInfo: KCSFile?
class JobRole : NSObject { //all NSObjects in Kinvey implicitly implement KCSPersistable var entityId: String? //Kinvey entity _id var name: String? var date: NSDate? var fileInfo: KCSFile?
Denny Tsai
From the documentation all the properties are declared with Optional types, but it seems like since the library is using NSJSONSerialization, the properties can't actually have nil values. So does that mean when I'm about the save the object I need to make sure all the properties need to have values? How do I have some properties that are optional?
Right now I'm overriding valueForKey: method to make the property return NSNull if the property is nil. Just wondering if there's a better way to do this or maybe I'm doing something totally wrong and the library should be handling optional properties automatically.
Here's the workaround code that I have currently:
This seems to be working fine.
Here is a sample:
class JobRole : NSObject { //all NSObjects in Kinvey implicitly implement KCSPersistable
var entityId: String? //Kinvey entity _id
var name: String?
var date: NSDate?
var fileInfo: KCSFile?
override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
return [
"entityId" : KCSEntityKeyId, //the required _id field
"name" : "name",
"date" : "date",
"fileInfo" : "fileInfo"
]
}
}
let jobRole1 = JobRole()
jobRole1.name = "testjob44441111"
jobRole1.date = nil
jobRole1.fileInfo = nil
var arr: NSMutableArray = []
arr.addObject(jobRole1)
store.saveObject(arr,
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 '%d objects.", (objectsOrNil.count))
}
},
withProgressBlock: nil )
The saveObject() works fine.
Is there any specific inputs when you are seeing the issue?
Also what version of KinveyKit are you using?
Thanks,
Pranav
Kinvey Support
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstPranav J
This seems to be working fine.
Here is a sample:
class JobRole : NSObject { //all NSObjects in Kinvey implicitly implement KCSPersistable
var entityId: String? //Kinvey entity _id
var name: String?
var date: NSDate?
var fileInfo: KCSFile?
override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
return [
"entityId" : KCSEntityKeyId, //the required _id field
"name" : "name",
"date" : "date",
"fileInfo" : "fileInfo"
]
}
}
let jobRole1 = JobRole()
jobRole1.name = "testjob44441111"
jobRole1.date = nil
jobRole1.fileInfo = nil
var arr: NSMutableArray = []
arr.addObject(jobRole1)
store.saveObject(arr,
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 '%d objects.", (objectsOrNil.count))
}
},
withProgressBlock: nil )
The saveObject() works fine.
Is there any specific inputs when you are seeing the issue?
Also what version of KinveyKit are you using?
Thanks,
Pranav
Kinvey Support
Pranav J
Just wanted to check with you if my suggestion was helpful. Let me know if not.
Thanks,
Pranav
Kinvey Support
-
Why do I get "Undefined symbols" errors when building with KinveyKit?
-
How do I register push tokens?
-
When using social login, to perform a log-out, do I need to log out of the social network, Kinvey, o
-
How can I assign additional properties to users?
-
Does KinveyKit support 64-bit ARM devices, such as iPhone 5s?
-
Authorization Token Invalid or Expired
-
BOOL and how it is stored in the database.
-
Offline saving throwing errors
-
Custom endpoint not able to form request object
-
Security through business logic
See all 437 topics