RestKit 0.20.0-pre3 错误-[NSManagedObjectContext saveToPersistentStore:]:无法识别的选择器发送到实例
Posted
技术标签:
【中文标题】RestKit 0.20.0-pre3 错误-[NSManagedObjectContext saveToPersistentStore:]:无法识别的选择器发送到实例【英文标题】:RestKit 0.20.0-pre3 Error -[NSManagedObjectContext saveToPersistentStore:]: unrecognized selector sent to instance 【发布时间】:2012-12-12 17:20:16 【问题描述】:我通过 CocoaPods 安装了 RestKit (0.20.0-pre3) 并按照文档说明对其进行了配置。执行 getObjectsAtPath 和 RestKit 时,我得到了映射:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSManagedObjectContext saveToPersistentStore:]:无法识别的选择器已发送到实例 0x9eaae40”
如果我设置断点,它会在第 278 行的 RKManagedObjectRequestOperation.m 处停止。
我将 RestKit 配置如下:
- (void)configureRestKit
//initialize RestKit
LLog(DEBUG_LEVEL, @"init RestKit");
NSString *baseURL;
// Log all HTTP traffic with request and response bodies
//RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
// Log debugging info about Core Data
RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);
//Loging about the Entity Mapping
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)
// enable activity indicator
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
if (local)
baseURL = @"http://xyz.net";
else
baseURL = @"http://XYZ.net";
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"];
_objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]];
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
_managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:_managedObjectModel];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XYZ.sqlite"];
//LLog(DEBUG_MODE,@"The object store: %@", _objectStore.description);
[_managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[_managedObjectStore createManagedObjectContexts];
[_managedObjectStore createPersistentStoreCoordinator];
_managedObjectContext = _managedObjectStore.persistentStoreManagedObjectContext;
_persistentStoreCoordinator = _managedObjectStore.persistentStoreCoordinator;
_managedObjectModel = _managedObjectStore.managedObjectModel;
_objectManager.managedObjectStore = _managedObjectStore;
// 2012-06-13T00:00:00+01:00
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'+01:00'"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
// Set it Globally
[RKObjectMapping setPreferredDateFormatter:dateFormatter];
LLog(DEBUG_LEVEL, @"DateFormater Test: %@", [dateFormatter stringFromDate:[NSDate date]]);
// Add Entity Mapping
RKEntityMapping *experienceMapping = [RKEntityMapping mappingForEntityForName:@"Experience" inManagedObjectStore:_managedObjectStore];
[experienceMapping addAttributeMappingsFromArray:@[@"experienceid", @"city", @"descr", @"lat", @"lng", @"number", @"price", @"street"]];
experienceMapping.identificationAttributes = @[@"experienceid"];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:experienceMapping pathPattern:@"/test.json" keyPath:@"experience" statusCodes:statusCodes];
[_objectManager addResponseDescriptor:responseDescriptor];
// ###### ONLY FOR TEST REASONS ######
NSManagedObject *test = [NSEntityDescription
insertNewObjectForEntityForName:@"Experience"
inManagedObjectContext:_managedObjectContext];
[test setValue:[NSNumber numberWithInt:1] forKey:@"experienceid"];
[test setValue:@"Testville" forKey:@"city"];
NSError *error;
if (![_managedObjectContext save:&error])
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
[_objectManager getObjectsAtPath:@"/test.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
// Handled with articleDescriptor
LLog(DEBUG_LEVEL, @"Test");
failure:^(RKObjectRequestOperation *operation, NSError *error)
// Transport error or server error handled by errorDescriptor
];
感谢您的帮助!
【问题讨论】:
没人知道吗? 【参考方案1】:这很可能表明您的链接器标志设置不正确。 -ObjC
链接器标志是加载类别方法所必需的。您是否覆盖了目标上的链接器标志?如果您在目标级别上有其他配置,则至少需要包含 $(inherited)
。在 Xcode 中查看设置时,您应该会看到 -ObjC
出现在链接器标志中。
【讨论】:
以上是关于RestKit 0.20.0-pre3 错误-[NSManagedObjectContext saveToPersistentStore:]:无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章