0

I'm trying to load a list of entities that have a many to one relationship with my user object. In this case, the User object contains a list of channels that need to be mapped (instantiate the channel ID only at first and then lazy load later) The API returns the IDs of the channels in an array and then I try to turn that array into an array of channels with RKRelationshipMapping. However, it keeps throwing

[<__NSCFString 0x7ff0f243ece0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key

Here is the JSON

{"id":"10","createDate":1412222248943,"modDate":1412222269588,"emailAddress":"chentsen1005@gmail.com","firstName":"test_account","lastName":"test_account_lastName","password":"****","referralCode":"password","channelSubscriptions":["1","2","3"]}

Here is my mapping code (in swift, but shouldn't matter)

class func mapReadUsersEndpoint(managedObjectStore : RKManagedObjectStore){
    var statusCodes = RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassSuccessful))
    var userGetRequestMapping = RKObjectMapping.requestMapping()
    userGetRequestMapping.addAttributeMappingsFromDictionary(["id":"id"])
    var userGetRequestDescriptor = RKRequestDescriptor(mapping: userGetRequestMapping,
        objectClass: User.self,
        rootKeyPath: nil,
        method : RKRequestMethod.GET)

    var userGetResponseMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: managedObjectStore)
    userGetResponseMapping.addAttributeMappingsFromDictionary(["firstName":"firstName",
        "lastName":"lastName",
        "emailAddress":"email",
        "referralCode":"referralCode",
        "password":"password"])

    var channelMapping = RKEntityMapping(forEntityForName: "Channel", inManagedObjectStore: managedObjectStore)
    channelMapping.addAttributeMappingsFromDictionary(["" : "channel_id"])
    var channelRelationshipMapping = RKRelationshipMapping(fromKeyPath: "channelSubscriptions", toKeyPath: "subscribedChannels", withMapping: channelMapping)
    userGetResponseMapping.addPropertyMapping(channelRelationshipMapping)

    var userGetResponseDescriptor = RKResponseDescriptor(mapping: userGetResponseMapping, method: RKRequestMethod.GET, pathPattern: "/resources/account/:id/", keyPath: nil, statusCodes: statusCodes)
    RKObjectManager.sharedManager().addRequestDescriptor(userGetRequestDescriptor)
    RKObjectManager.sharedManager().addResponseDescriptor(userGetResponseDescriptor)

and Finally, the error message

Performing mapping operation: <RKMappingOperation 0x7ff0f278ad00> for 'Channel' object. Mapping values from object 1 ({
HTTP =     {
    request =         {
        URL = "http://localhost:8080/resources/account/10/";
        headers =             {
            Accept = "application/json";
            "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
            "User-Agent" = "Hip/1 (iPhone Simulator; iOS 8.0; Scale/2.00)";
        };
        method = GET;
    };
    response =         {
        URL = "http://localhost:8080/resources/account/10/";
        headers =             {
            "Content-Type" = "application/json";
            Date = "Thu, 02 Oct 2014 04:22:41 GMT";
            Server = "Jetty(9.2.2.v20140723)";
            "Transfer-Encoding" = Identity;
        };
    };
};
mapping =     {
    collectionIndex = 0;
    rootKeyPath = "<null>";
};
}) 

to object <Channel: 0x7ff0f279f340> (entity: Channel; id: 0x7ff0f279f3b0 <x-coredata:///Channel/tBBBCA84F-8C28-4729-8E06-A93802EFE14E2> ; data: {
    backgroundColor = 0;
    backgroundImage = nil;
    channelDescription = nil;
    "channel_id" = nil;
    followers = 0;
    imageData = nil;
    isPublic = nil;
    name = nil;
    referralSource = nil;
    subscribers =     (
    );
    threads =     (
    );
}) with object mapping (null)
2014-10-02 00:22:41.789 Hip[99685:3770746] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7ff0f243ece0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key .'

I'm pretty lost here -- It looks like I've followed everything on the Github page. Any advice greatly appreciated. Thanks

Wain
  • 118,658
  • 15
  • 128
  • 151
Andy Tsen
  • 619
  • 2
  • 6
  • 19
  • This seems strange: channelMapping.addAttributeMappingsFromDictionary(["" : "channel_id"]) Can you explain what you're trying to do with that line? – Tom Erik Støwer Oct 02 '14 at 05:46
  • I figured it out. I had to add a nil property mapping on the actual Channel mapping itself. – Andy Tsen Oct 02 '14 at 14:01

0 Answers0