Restkit - 无法在 NSManagedObject class 上调用指定的初始化程序

Restkit-Failed to call designated initializer on NSManagedObject class

我将 Restkit 用于扩展 NSManagedObject 的 class。 我知道休息工具包本身具有从网络获取中保存到核心数据的功能。但是,由于以下原因,我无法使用该功能:

所以我想做的是让我的模型 classes 扩展 nsmanaged 对象,并在需要时保存它。但我收到此错误:

CoreData: error: Failed to call designated initializer on NSManagedObject class

有办法解决这个问题吗?

我是这样从服务器获取的:

@implementation API_Login
+(void)performLoginWithEmail:(NSString*)email
                withPassword:(NSString*)password
                     success:(void (^)(Token* user) )success
                     failure:failureblock failure{

    RKObjectManager * objectManager = [APIHelper getRestObjectManager];
    RKObjectMapping *tokenMapping = [RKObjectMapping mappingForClass:[Token class]];

//add mapping for token
    [tokenMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"token"]];
    RKResponseDescriptor *responseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:tokenMapping
                                                 method:RKRequestMethodGET
                                            pathPattern:nil
                                                keyPath:nil
                                            statusCodes:[NSIndexSet indexSetWithIndex:200]];

    [objectManager addResponseDescriptor:responseDescriptor];


//    add mapping for error
    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[Error class]];
    [errorMapping addAttributeMappingsFromDictionary:@{@"message":@"message",@"badRequest":@"badRequest"}];
    RKResponseDescriptor *errorResponseDescriptor =
    [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                 method:RKRequestMethodGET
                                            pathPattern:nil
                                                keyPath:nil
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];

    [objectManager addResponseDescriptor:errorResponseDescriptor];



    NSDictionary *queryParams =  @{@"email" : email,
                                   @"password" : password,
                                   };

    [objectManager postObject:nil
                         path:@"/users/api/login"
                   parameters:queryParams
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                          Token* token =  (Token*)[mappingResult firstObject] ;
//                          [AppDelegateHandle setToken:token];
                          success(token);
                      } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          NSArray* e = [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey];
                          Error *err = (Error*)[e objectAtIndex:0];
                          NSLog(@"%@",[err.badRequest allValues] );
                          failure(operation,error);
                      }];


}
@end

我的令牌 class 看起来像:

@interface Token : NSManagedObject
@property NSString* token;
@end

我的 api 回复看起来像:

{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL3JlbWluZGVyLmRldlwvdXNlcnNcL2FwaVwvbG9naW4iLCJpYXQiOjE0Njg1OTM3NjYsImV4cCI6MTQ2OTE5Mzc2NiwibmJmIjoxNDY4NTkzNzY2LCJqdGkiOiIxMDc3ZjBhY2ViYTFjOWZjZWNhYjkyMzYyOTA0ZmI4NSJ9.I6FHJLCCHr3EHQa8HgaDqxQMjF1HVyA5AymPjvBGDrM"
}

当我更改 Token 以扩展 NSObject 而不是 NSManagedObject 时,一切正常。可能是什么问题?

发生这种情况是因为您使用的是 RKObjectMapping 而不是 RKEntityMapping,如果您使用的是 NSManagedObject.

的子类,则这是必需的

如果您不打算将其直接添加到上下文中,则不能使用 NSManagedObject 的子类。

如果您的请求只有一个令牌,那么我可能不会为 RestKit 而烦恼,但在一般情况下,我会映射到 NSDictionary,密钥与您的托管对象相同 类 然后当你想创建你的托管对象时,你可以这样做,然后 'import' 使用 setValuesForKeysWithDictionary:.

将数据发送给它们