1

这个错误似乎出现了很多次。这个问题最突出的答案似乎是 RestKit 0.20 — CoreData: error: Failed to call specified initializer on NSManagedObject class

但是,发布的解决方案对我不起作用。但是让我们从头开始:

我想将文件上传到服务器。我想解析响应以确保它已成功上传。如果没有,我稍后再试。NSNumber *sentToServer我的模型有一个为此目的调用的布尔变量(在 CoreData 中,BOOL保存为NSNumber

因为我不知道发送是否会成功,所以我总是先将实体保存到我的 CoreData 模型中,然后再通过 RestKit 发送它。

当 POST 请求成功时,服务器会返回一个 JSON 字符串,如下所示:

{"id":14,"created":true}

请注意,当然,尚未在 CoreData 模型上设置 ID,因此两者id都是created我的模型的新信息。

这是我的模型:

@interface Recording : NSManagedObject

@property (nonatomic, retain) NSString * audioFilePath;
@property (nonatomic, retain) NSDate * createdAt;
@property (nonatomic, retain) NSString * deviceId;
@property (nonatomic, retain) NSString * deviceType;
@property (nonatomic, retain) NSNumber * recordingId;
@property (nonatomic, retain) NSNumber * sentToServer;

@end

这是我的设置方式:

NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HTRecording" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AudioModel.sqlite"];
NSError *error = nil;

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

if (![managedObjectStore addSQLitePersistentStoreAtPath:storePath
                                 fromSeedDatabaseAtPath: nil
                                      withConfiguration:nil
                                                options:options
                                                  error:&error]) {
    NSAssert(NO, @"Managed object store failed to create persistent store coordinator: %@", error);
}
[managedObjectStore createManagedObjectContexts];

// Configure MagicalRecord to use RestKit's Core Data stack
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:managedObjectStore.persistentStoreCoordinator];
[NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext];
[NSManagedObjectContext MR_setDefaultContext:managedObjectStore.mainQueueManagedObjectContext];

NSString *databaseUrl = @"http://localhost:3000";
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:databaseUrl]];
objectManager.managedObjectStore = managedObjectStore;
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

// Setup mapping
RKEntityMapping* recordingRequestMapping = [RKEntityMapping mappingForEntityForName:@"Recording" inManagedObjectStore:managedObjectStore];
[recordingRequestMapping addAttributeMappingsFromDictionary:@{
 @"device_id": @"deviceId",
 @"device_type": @"deviceType"     }];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[recordingRequestMapping inverseMapping]
                                                                               objectClass:[Recording class]
                                                                               rootKeyPath:@"recording"];
[objectManager addRequestDescriptor:requestDescriptor];

RKEntityMapping* recordingResponseMapping = [RKEntityMapping mappingForEntityForName:@"Recording" inManagedObjectStore:managedObjectStore];
[recordingResponseMapping addAttributeMappingsFromDictionary:@{
 @"created": @"sentToServer",
 @"id": @"recordingId"}];
recordingResponseMapping.identificationAttributes = @[@"recordingId"];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:recordingResponseMapping
                                                                                   pathPattern:@"/audio.json"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];

首先,对于应该很常见的东西(只是初始化 RestKit 和 MagicalRecord),这是一个令人惊讶的冗长设置代码。但我离题了...

整个上传过程正常。请求映射很好,所有内容都存储在服务器上,包括附件。请注意,请求映射不包含文件,因为它们是通过多部分 post 方法发送的。同样,该部分工作正常。下面是上传文件的代码:

RKObjectManager *manager = [RKObjectManager sharedManager];
NSMutableURLRequest *request;

request = [manager multipartFormRequestWithObject:recording
                                           method:RKRequestMethodPOST
                                             path:@"audio.json" 
                                       parameters:nil
                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:[[NSFileManager defaultManager] contentsAtPath:recording.audioFilePath]
                                    name:@"recording[audio]"
                                fileName:[recording.audioFilePath lastPathComponent]
                                mimeType:@"audio/m4a"];
    }];

RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request
                                                                         success:^(RKObjectRequestOperation *op, RKMappingResult *mapping) {
                                                                             NSLog(@"successfully saved file on the server");
                                                                         }
                                                                         failure:^(RKObjectRequestOperation *op, NSError *error){
                                                                             NSLog(@"some weird error");
                                                                         }];


[manager enqueueObjectRequestOperation:operation];

将响应映射到对象变得困难。如果没有匹配的响应映射,日志输出会包含无法处理响应的错误(惊喜)。但是,使用匹配的响应映射时,应用程序会崩溃。这是堆栈跟踪:

#11 0x2f6482d6 in -[NSObject(NSKeyValueCoding) valueForKeyPath:] ()
#12 0x00113b9a in -[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:436
#13 0x00114e40 in -[RKMappingOperation applyAttributeMapping:withValue:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:536
#14 0x00115fd4 in -[RKMappingOperation applyAttributeMappings:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:577
#15 0x0011c988 in -[RKMappingOperation main] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:948
#16 0x2f655d0a in -[__NSOperationInternal _start:] ()
#17 0x0010cd5a in -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMapperOperation.m:256
#18 0x0010b6aa in -[RKMapperOperation mapRepresentation:atKeyPath:usingMapping:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMapperOperation.m:176
#19 0x0010ddc8 in -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMapperOperation.m:314
#20 0x0010e552 in -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMapperOperation.m:359
#21 0x0010ee72 in -[RKMapperOperation main] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/ObjectMapping/RKMapperOperation.m:398
#22 0x2f655d0a in -[__NSOperationInternal _start:] ()
#23 0x00100e70 in -[RKObjectResponseMapperOperation performMappingWithObject:error:] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/Network/RKResponseMapperOperation.m:345
#24 0x000ff4e6 in -[RKResponseMapperOperation main] at /Users/earthnail/development/RestkitFileUpload/Pods/RestKit/Code/Network/RKResponseMapperOperation.m:297
#25 0x2f655d0a in -[__NSOperationInternal _start:] ()

SIGABRT调试器以in停止的行[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:]是:

id currentValue = [self.destinationObject valueForKeyPath:keyPath];

请注意,当我按照https://stackoverflow.com/a/13734348/487556中的建议设置断点时,永远不会到达该断点。这意味着响应映射的路径没有问题。

我可以想象这里出了几件事:

  1. RestKit 假设还没有 CoreData 实体,并且想要创建一个。这会导致重复的实体(这将是一个无赖!),但它仍然不应该崩溃,因为模型的所有字段都是optional
  2. RestKit 尝试根据 ID 查找 CoreData 实体(因为这是标识符变量),但当然 ID 尚未设置。
  3. 还有什么...

有什么建议么?

4

2 回答 2

1

我知道你问已经有一段时间了,也许你已经找到了自己的方式,但我只是遇到了同样的情况并找到了解决方案。

问题在于您在objectRequestOperationWithRequest:success:failure:使用 NSManagedObjects 时使用了该方法。因此,RestKit 在映射操作期间尝试实例化一个常规的 NSObject,这会导致崩溃。

尝试managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:改用,事情应该会更好。

于 2014-04-28T09:33:31.530 回答
0

问题在于响应描述和请求中的匹配路径。为了使它起作用,它们中的斜线应该是相同的,@"/audio.json"否则@"audio.json"RestKit 看不到一个匹配另一个。

于 2013-11-02T12:13:36.787 回答