Question

Possible duplication : RestKit Core Data 'managedObjectStore is nil'

Could somebody explain me why when I run my application I get error like :

  • Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: managedObjectStore' *** First throw call stack

It happened to me only in my MainViewController when I'm calling the following method:

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

When I'm calling this method in other ViewControllers everything works fine. It looks like my managedObjectStore is nil, but I don't know what is the reason for that... Please take a look at my DateModel code and share with me your ideas:

@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];
}

In my AppDelegate method I just simply setup my store.

-(void)setupCoreData
{
[[DateModel sharedDataModel]setup];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupCoreData];
}

When I run my application I'm getting error like on the screen below: 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;
}
Was it helpful?

Solution

loadLocationsOfWearers is being called too soon (before setupCoreData is called). Call it later, or call setupCoreData when the data model singleton is created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top