Unable to store in Core Data from RestKit because of NSUnknownKeyException

和自甴很熟 提交于 2019-12-13 17:26:15

问题


I have a web service that returns data as such:

{"facebook_friends":[{"name":"Jennifer","id":"38493"},{"name":"Sarah","id":"363"},

And here is my code:

RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"MyApp.sqlite"];
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURLString:@"http://192.168.85.128"];
objectManager.objectStore = objectStore;

RKManagedObjectMapping *friendMapping = [RKManagedObjectMapping mappingForClass:[Friend class] inManagedObjectStore:objectStore];
[friendMapping mapKeyPath:@"id" toAttribute:@"facebook_id"];
[friendMapping mapKeyPath:@"name" toAttribute:@"name"];
friendMapping.primaryKeyAttribute = @"facebook_id";

[[RKObjectManager sharedManager].mappingProvider setMapping:friendMapping forKeyPath:@"facebook_friends"];

[objectManager loadObjectsAtResourcePath:@"/sharing/friends_facebook.json" delegate:self];

But I am getting this error:

2012-06-05 09:53:14.098 promobust[10071:1520b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSManagedObject 0x6d153b0> setValue:forUndefinedKey:]: the entity Friend is not key value coding-compliant for the key "facebook_id".'

I don't understand what is wrong given that each entity has an id property - anyone see what I am doing wrong?

Update: Friend Class:

@interface Friend : NSManagedObject
    @property (nonatomic, retain) NSNumber* facebook_id;
    @property (nonatomic, retain) NSString* name;

- (BOOL)validateName:(id *)ioValue error:(NSError **)outError;

@end

@implementation Friend
    // We use @dynamic for the properties in Core Data
    @dynamic facebook_id;
    @dynamic name;


- (BOOL)validateName:(id *)ioValue error:(NSError **)outError {
    NSLog(@"Validating Friend");
    return YES;
}

Thanks!


回答1:


It would help if I hadn't removed the CoreData model from my application!

It is still not working but with a different error - I will close this issue.



来源:https://stackoverflow.com/questions/10898716/unable-to-store-in-core-data-from-restkit-because-of-nsunknownkeyexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!