Restkit, Swift, Core data one to many

100 Views Asked by At

I just can't figure out this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil

Here is some sample JSON

[{"storeId":"22","storeName":"Jewel Osco","storeAddress1":"890 N Western Ave","storeAddress2":"","storeCity":"Lake Forest","storeState":"IL","storeZipCode":"","storeGfCount":"71","storeListCount":"2","storeDistance":"1.13","products":[{"storeId":"22","productId":"61","upcCode":"00090800000751","brandDesc":"Herb Pharm","productName":"Herb Pharm Black Elderberry Herbal Glycerite Extract"},{"storeId":"22","productId":"64","upcCode":"00698997807308","brandDesc":"Udi's","productName":"Udi's Soft N' Chewy Granola Bars Chocolate Chip - 5 CT"}]}]

It is an array of stores with a one to many array of products.

If I run my query without the relationship it runs perfectly. However when I add in the relationship it fails.

Here is the related mapping:

    class func defaultMapping(objectManager:RKObjectManager!) -> RKEntityMapping{

    let managedObjectStore:RKManagedObjectStore = objectManager.managedObjectStore!
    let defaultMapping = RKEntityMapping(forEntityForName: "LocatorStoreProduct", inManagedObjectStore: managedObjectStore)

    defaultMapping.identificationAttributes = ["productId","storeId"]

    let mappingDictionary:[String:String] = [
        "productId":"productId" ,
        "storeId":"storeId" ,
        "upcCode":"upcCode" ,
        "brandDesc":"brand" ,
        "productName":"productName"
    ]


    defaultMapping.addAttributeMappingsFromDictionary(mappingDictionary)

    return defaultMapping

}

Here is the assignment of the mapping:

class func defaultMapping(objectManager:RKObjectManager!) -> RKEntityMapping{

    let managedObjectStore:RKManagedObjectStore = objectManager.managedObjectStore!
    let defaultMapping = RKEntityMapping(forEntityForName: "LocatorStore", inManagedObjectStore: managedObjectStore)

    defaultMapping.identificationAttributes = ["extId"]

    let mappingDictionary:[String:String] = [
        "storeId":"extId" ,
        "storeName":"storeName" ,
        "storeAddress1":"address1" ,
        "storeAddress2":"address2" ,
        "storeCity":"city" ,
        "storeState":"state" ,
        "storeZipCode":"zipCode" ,
        "stoerGfCount":"totalCount",
        "storeListCount":"inListCount",
        "storeDistance":"distance" 
    ]

    defaultMapping.addAttributeMappingsFromDictionary(mappingDictionary)


    //HERE IS MY RELATIONSHIP MAPPING:

    let lspMapping = LocatorStoreProduct.defaultMapping(objectManager)
    let relationshipMapping = RKRelationshipMapping(fromKeyPath: "products", toKeyPath: "locatorStoreProducts", withMapping: lspMapping)

    defaultMapping.addPropertyMapping(relationshipMapping)

    return defaultMapping

}

Been staring at this for hours and can't see what key I am missing

1

There are 1 best solutions below

0
On

The code is correct. I had renamed my obj from LocatorStoreProducts to LocatorStoreProduct. Missed an obvious place in the refactor