RestKit Mapping无效参数不满足:managedObjectStore

时间:2014-05-05 05:07:41

标签: ios objective-c core-data restkit restkit-0.20

可能重复: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];
}

当我在其他ViewControllers中调用此方法时,一切正常。 看起来我的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];
}

当我运行我的应用程序时,我收到的错误就像在下面的屏幕上: Error during running my app

+(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;
}

1 个答案:

答案 0 :(得分:0)

loadLocationsOfWearers过早被调用(在setupCoreData被调用之前)。稍后调用它,或在创建数据模型单例时调用setupCoreData