- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个错误似乎出现了很多次。这个问题最突出的答案似乎是
RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class
但是,发布的解决方案对我不起作用。但是让我们从头开始:
我想将文件上传到服务器。我想解析响应以确保它已成功上传。如果没有,我稍后再试。我的模型有一个名为 NSNumber *sentToServer
的 bool 变量。为此目的(在 CoreData 中,BOOL
保存为 NSNumber
)。
因为我不知道发送是否会成功,所以我总是先将实体保存到我的 CoreData 模型中,然后再通过 RestKit 发送它。
当 POST 请求成功时,服务器会返回一个 JSON 字符串,如下所示:
{"id":14,"created":true}
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];
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
停止的行在
[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:]
是:
id currentValue = [self.destinationObject valueForKeyPath:keyPath];
最佳答案
我知道你问已经有一段时间了,也许你已经找到了自己的方式,但我只是遇到了同样的情况并找到了解决方案。
问题来自您使用 objectRequestOperationWithRequest:success:failure:
的事实。使用 NSManagedObjects 时的方法。因此,RestKit 在映射操作期间尝试实例化一个常规的 NSObject,这会导致崩溃。
尝试使用 managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:
相反,事情应该会更好。
关于ios - RestKit 0.20 — 核心数据 : error: Failed to call designated initializer on NSManagedObject class (again),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258698/
我正在尝试将 keras.initializers 引入我的网络,following this link : import keras from keras.optimizers import RMS
我正在为程序创建某种前端。为了启动程序,我使用了调用 CreateProcess(),其中接收到一个指向 STARTUPINFO 结构的指针。初始化我曾经做过的结构: STARTUPINFO star
我已经模板化了 gray_code 类,该类旨在存储一些无符号整数,其基础位以格雷码顺序存储。这里是: template struct gray_code { static_assert(st
我已经查看了之前所有与此标题类似的问题,但我找不到解决方案。所有错误都表明我没有初始化 ArrayList。我是否没有像 = new ArrayList 这样初始化 ArrayList? ? impo
当涉及到 Swift 类时,我对必需的初始化器和委托(delegate)的初始化器有点混淆。 正如您在下面的示例代码中所见,NewDog 可以通过两种方式中的一种进行初始化。如您所见,您可以通过在初始
几天来我一直在为一段代码苦苦挣扎。我在运行代码时收到的错误消息是: 错误:数组初始值设定项必须是初始值设定项列表 accountStore(int size = 0):accts(大小){} 这里似乎
我想返回一个数组,因为它是否被覆盖并不重要,我的方法是这样的: double * kryds(double linje_1[], double linje_2[]){ double x = linje
尝试在 C++ 中创建一个简单的 vector 时,出现以下错误: Non-aggregates cannot be initialized with initializer list. 我使用的代码
如何在构造函数中(在堆栈上)存储初始化列表所需的临时状态? 例如,实现这个构造函数…… // configabstraction.h #include class ConfigAbstraction
我正在尝试编写一个 native Node 插件,它枚举 Windows 机器上的所有窗口并将它们的标题数组返回给 JS userland。 但是我被这个错误难住了: C:\Program Files
#include using namespace std; struct TDate { int day, month, year; void Readfromkb() {
我很难弄清楚这段代码为何有效。我不应该收到“数组初始值设定项必须是初始值设定项列表”错误吗? #include class B { public: B() { std::cout << "B C
std::map m = { {"Marc G.", 123}, {"Zulija N.", 456}, {"John D.", 369} }; 在 Xcode 中,我将 C+
为了帮助你明白这一点,我给出了我的代码:(main.cpp),只涉及一个文件。 #include #include using namespace std; class test{ public
这在 VS2018 中有效,但在 2008 中无效,我不确定如何修复它。 #include #include int main() { std::map myMap = {
我有一个类: #include class Object { std::shared_ptr object_ptr; public: Object() {} template
我正在为 POD、STL 和复合类型(如数组)开发小型(漂亮)打印机。在这样做的同时,我也在摆弄初始化列表并遇到以下声明 std::vector arr{ { 10, 11, 12 }, { 20,
我正在使用解析实现模型。 这是我的代码。 import Foundation import UIKit import Parse class User { var objectId : String
我正在观看 Java 内存模型视频演示,作者说与 Lazy Initialization 相比,使用 Static Lazy Initialization 更好,我不清楚他说的是什么想说。 我想接触社
如果您查看 Backbone.js 的源代码,您会看到此模式的多种用途: this.initialize.apply(this, arguments); 例如,这里: var Router =
我是一名优秀的程序员,十分优秀!