Start a new topic

Relational Data - Invalid type in JSON write (KCSKinveyRef)

I am new to Kinvey but I am experiencing difficulties using Relational Data


- I am using KinveyKit-1.40.4

- I am following the steps outlined in http://devcenter.kinvey.com/ios/guides/datastore

- I am using KCSLinkedAppdataStore

- I have confirmed that both entities are configured correctly and save individually but when I attempt to update them with a relationship (single or multiple) I get an exception.


'NSInvalidArgumentException', reason: 'Invalid type in JSON write (KCSKinveyRef)'


Can anyone suggest a possible solution? I am missing an important step?


2 people have this question

Even 1-to-1 relational data has issues. Attempting to load an entity with saved relational data using the loadObjectWithID method causes a crash 


reason: 'Invalid type in JSON write (EventPlanner.DressCode)


However fetching the same entity using an exact match query works fine

1.40.5 seems to have fixed relational data for 1-to-1 relationships e.g. user: KCSUser = KCSUser.activeUser() the problem occurs when attempting to store multiple references in an array e.g. users: [KCSUser] = [KCSUser.activeUser()


static override func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {

 return ["user" : KCSUserCollectionName, "users" : KCSUserCollectionName]

 }




1 person likes this

 This is my code snippet: KCSQuickle has an array to KCSQuickleItems -- both are collections in by Data Store.

class KCSQuickle: NSObject {
    
    static let collectionName = "Quickles"
    static let titleField = "title"
    static let itemsField = "items"
    
    var entityId: String?               // Kinvey entity _id
    var title: String                   // Title of the quickle
    var contributors: [KCSUser]?        // Array of contributors who liked
    var likes: [KCSUser]?               // Array of users who liked
    var metadata: KCSMetadata?          // Create, Updated and Creator
    var items: [KCSQuickleItem]      // Referenced of type KCSQuickleItem

    
    private override init() {
        title = ""
        items = [KCSQuickleItem]()
        super.init()
    }
    
    convenience init(titleString: String) {
        self.init()
        title = titleString
    }
   
    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId,
            "title" : KCSQuickle.titleField,
            "items" : KCSQuickle.itemsField,
            "metadata" : KCSEntityKeyMetadata,
        ]
    }
    
    static override func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {
        return [
            // backend to collection name
            KCSQuickle.itemsField : KCSQuickleItem.collectionName
        ]
    }
    
    static override func kinveyObjectBuilderOptions() -> [NSObject : AnyObject]! {
        return [
            KCS_REFERENCE_MAP_KEY : [
                // property name to object
                "items" : KCSQuickleItem.self
            ]
        ]
    }
    
    override func referenceKinveyPropertiesOfObjectsToSave() -> [AnyObject]! {
        // array of backend property names to save
        return [ KCSQuickle.itemsField ]
    }
}



class KCSQuickleItem: NSObject {
    
    static let collectionName = "QuickleItem"
    static let itemDescriptionField = "item_description"
    
    var entityId: String?           // Kinvey entity _id
    var itemDescription: String?

    override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
        return [
            "entityId" : KCSEntityKeyId,
            "itemDescription" : KCSQuickleItem.itemDescriptionField
        ]
    }

 I try to save it as:


 

let store = KCSQuickle.getLinkedStore()
                        store.saveObject(self.quickle, withCompletionBlock: { (saveResults, saveError) -> Void in
if (saveError == nil) {
// do stuff
}
}, withProgressBlock: nil)

 

I am still getting the exact same error when trying to create related objects and save. I am using 1.40.5 as well.

I have update to 1.40.5 and I am able to add singular entity relationships e.g. event.invitee = firstInvitation however when I attempt to map to an NSMutableSet of multiple entities e.g. event.invitees = [firstInvitation] I receive the error


'Invalid type in JSON write (KCSKinveyRef)'


Do I need to change to configuration of kinveyPropertyToCollectionMapping,  kinveyObjectBuilderOptions or referenceKinveyPropertiesOfObjectsToSave to solve this issue?


Thanks


P.S. Do anyone have a generic sample projects that demonstrates how to setup this up?


1 person likes this

Hi there Richard, 


There was a bug in version 1.40.4 of our iOS library which is updated in the most recent update.   Can you please give that a shot and try it out?


Thanks,


http://devcenter.kinvey.com/ios/downloads

Login or Signup to post a comment