RestKit 映射无效参数不满足:managedObjectStore
Posted
技术标签:
【中文标题】RestKit 映射无效参数不满足:managedObjectStore【英文标题】:RestKit Mapping Invalid parameter not satisfying: managedObjectStore 【发布时间】:2014-05-05 05:07:41 【问题描述】:可能重复:RestKit Core Data 'managedObjectStore is nil'
谁能解释一下为什么当我运行我的应用程序时会出现如下错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:managedObjectStore” *** 首先抛出调用堆栈
当我调用以下方法时,它只发生在我的 MainViewController 中:
-(void)loadLocationsOfWearers
RKManagedObjectStore *store = [[DateModel sharedDataModel] objectStore];
NSIndexSet *statusCodeSet= RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKMapping *mapping = [MappingProvider watchesMapping];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"watch" statusCodes:statusCodeSet];
NSURL *url = [NSURL URLWithString:kSERVER_ADDR];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKManagedObjectRequestOperation *operation =[[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectCache = store.managedObjectCache;
operation.managedObjectContext = store.mainQueueManagedObjectContext;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
_wearerList = mappingResult.array;
NSLog(@"Results:\n %@",mappingResult.dictionary);
failure:^(RKObjectRequestOperation *operation, NSError *error)
NSLog(@"ERROR: %@", error);
NSLog(@"Response: %@", operation.HTTPRequestOperation.responseString);
];
[operation start];
当我在其他 ViewController 中调用此方法时,一切正常。 看起来我的 managedObjectStore 为零,但我不知道这是什么原因...... 请查看我的 DateModel 代码并与我分享您的想法:
@implementation DateModel
+ (id)sharedDataModel
static DateModel *__sharedDataModel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
__sharedDataModel = [[DateModel alloc] init];
);
return __sharedDataModel;
- (NSManagedObjectModel *)managedObjectModel
return [NSManagedObjectModel mergedModelFromBundles:nil];
- (id)optionsForSqliteStore
return @
NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES
;
- (void)setup
self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Data.sqlite"];
NSLog(@"Setting up store at %@", path);
NSError *error;
[self.objectStore addSQLitePersistentStoreAtPath:path
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:[self optionsForSqliteStore]
error:&error];
[self.objectStore createManagedObjectContexts];
self.objectStore.managedObjectCache =[[RKInMemoryManagedObjectCache alloc]initWithManagedObjectContext:self.objectStore.persistentStoreManagedObjectContext];
[RKManagedObjectStore setDefaultStore:self.objectStore];
在我的 AppDelegate 方法中,我只是简单地设置了我的商店。
-(void)setupCoreData
[[DateModel sharedDataModel]setup];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[self setupCoreData];
当我运行我的应用程序时,出现如下屏幕所示的错误:
+(RKMapping *)watchesMapping
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Watches" inManagedObjectStore:[[DateModel sharedDataModel]objectStore]];
[mapping addAttributeMappingsFromDictionary:@
@"id": @"watch_id",
@"altitude":@"altitude",
@"battery_life":@"battery_life",
@"button_press_time":@"button_press_time",
@"charging_status":@"charging_status",
@"gmaps":@"gmaps",
@"id_addr":@"id_addr",
@"last_keep_alive":@"last_keep_alive",
@"last_update_time":@"last_update_time",
@"latitude":@"latitude",
@"longitude":@"longitude",
@"location":@"location",
@"network":@"network",
@"phonewatchno":@"phonewatchno",
@"rssi":@"rssi",
@"short_imei":@"short_imei",
@"updated_at":@"updated_at",
@"voltage":@"voltage",
@"waerer_id":@"wearer_id",
@"updated_at":@"updated_at",
@"token":@"token"
];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"alerts" toKeyPath:@"alerts" withMapping:[MappingProvider alertsMapping]]];
return mapping;
【问题讨论】:
显示MappingProvider watchesMapping
的代码
感谢您抽出宝贵时间更新我的问题
【参考方案1】:
loadLocationsOfWearers
被调用太快(在setupCoreData
被调用之前)。稍后调用,或在创建数据模型单例时调用setupCoreData
。
【讨论】:
NSEntityDescription *destinationEntity = [relationshipDescription destinationEntity]; 在我的单例中调用 setupCoreData 时,我收到 badAccess 错误,并且我的应用程序正在停止调试此行: NSEntityDescription *destinationEntity = [relationshipDescription destinationEntity]; loadLocationsOfWearers 方法在 viewDidLoad 方法中被调用,所以我和它只是在那里调用的一种方法。任何其他想法如何解决它? 异常名称和消息是什么?改变做事的顺序才是正确的解决方案 代码中有恶臭是我的错。我在其他方法中有 dispatch_asynch 块,这是此错误的主要原因。谢谢你的帮助,我真的很感激。干杯以上是关于RestKit 映射无效参数不满足:managedObjectStore的主要内容,如果未能解决你的问题,请参考以下文章
RestKit - 找不到 keyPath 的对象映射:''