Start a new topic
Answered

Can't save KCSPersistable objects with nil properties

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
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?

    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

 


Answer
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?

    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

 

Denny,

Just wanted to check with you if my suggestion was helpful. Let me know if not.

Thanks,
Pranav
Kinvey Support

 

Login or Signup to post a comment