Search code examples
iphoneioscore-dataentity-relationshipstackmob

getting crash while Accessing relationship objects One to One coredata/stackmob


I have 2 tables Assets and Rewards there are related Assets<---->Rewards, with relationshipname assetrewards, when i am trying to access the rewards table field likewise

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Assets"]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"assetsId == %@",str]; 
[request setPredicate:predicate]; 
[self.managedObjectContext executeFetchRequest:request onSuccess:^(NSArray *assetsArr) { 
for (Assets *assets in assetsArr) { 
NSLog(@"assets %@",[assets valueForKey:@"assetrewards.rewardsId"]); 
}
} onFailure:^(NSError *error) {

        NSLog(@"Error fetching: %@", error);

    }];

then i am getting a crash report saying

:  Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Assets 0x9fb9380> valueForUndefinedKey:]: the entity Assets is not key value coding-compliant for the key "assetrewards.rewardsId".'

but when i am checking like wise

NSLog(@"assets %@",[assets valueForKey:@"assetrewards"]);

i am getting output like this :

assets <Rewards: 0xad8a4f0> (entity: Rewards; id: 0x9fe4bf0 <x-coredata://B2D33481-2AF2-4C6D-9428-33BDCF653881-15578-00004A2B32ACC29E/Rewards/pc05848de529d4cbab6eb4475a5f9f591> ; data: <fault>)

I want access the all the values of the rewards fields with the relationship/ or for that particular asset as there is a reward related i want to get the details of the Rewards


Solution

  • You can't use valueForKey: on a multi-step key path like assetrewards.rewardsId. You have to use valueForKeyPath:. As written, the code is looking for a key named assetrewards.rewardsId, not a key named assetrewards that leads to an object with a key named rewardsId.