This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.

I get the error when trying to save the context...

This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.


here is my code...


//
//  CoreDataStack.m
//  Expenses
//
//  Created by Neerav Kothari on 11/04/14.
//  Copyright (c) 2014 Neerav Kothari. All rights reserved.
//

#import "CoreDataStack.h"
#import <CoreData/CoreData.h>


@interface CoreDataStack ()

@end

@implementation CoreDataStack

@synthesize managedObjectModel=_managedObjectModel, managedObjectContext=_managedObjectContext, persistentStoreCoordinator=_persistentStoreCoordinator;


#pragma mark - Initializer

-(id)init
{
    if (self = [super init])
    {
        _managedObjectContext = [self managedObjectContext];
    }
    
    return self;
}


#pragma mark - Save and Fetch
- (void)saveContext
{
    NSError *error;
    if (_managedObjectContext != nil) {
        if ([_managedObjectContext hasChanges] && ![_managedObjectContext save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.
             
             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}
#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    [self persistentStoreCoordinator];
    if (_persistentStoreCoordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:_persistentStoreCoordinator];
    }
    return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Pace" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil)
    {
        NSLog(@"store url:%@",[_persistentStoreCoordinator.persistentStores[0] URL]);
        return _persistentStoreCoordinator;
    }
    
    NSURL *storeURL;
    NSError *error = nil;
    
    
    
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    
    storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Pace.sqlite"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:storeURL.path])
    {
        _persistentStore = [[NSPersistentStore alloc] initWithPersistentStoreCoordinator:_persistentStoreCoordinator configurationName:nil URL:storeURL options:nil];
    }
    else
    {
        _persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
    }
    
    if (!_persistentStore)
    {
        /*
         Replace this implementation with code to handle the error appropriately.
         
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
         
         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.
         
         
         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
         
         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
         
         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
         
         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
         
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    else
    {
        NSLog(@"store url %@",[[_persistentStore URL] path]);
    }
    
    
    return _persistentStoreCoordinator;
}




#pragma mark - Application's documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end

Replies

Inspecting through breakpoints, after the following statement...


    if ([[NSFileManager defaultManager] fileExistsAtPath:storeURL.path])
    {
        _persistentStore = [[NSPersistentStore alloc] initWithPersistentStoreCoordinator:_persistentStoreCoordinator configurationName:nil URL:storeURL options:nil];
    }
    else
    {
        _persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];
    }

... the _persistentStoreCoordinator doesn't show any persistent stores. it shows an empty array.

solutions elsewhere on this error indicate managedobjectcontext object on background thread does this. my moc is on main thread.


    if (_persistentStoreCoordinator != nil) {  
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];  
        [_managedObjectContext setPersistentStoreCoordinator:_persistentStoreCoordinator];  }

i checked with breakpoints. the stack objects are being iniatilized and the reference to the stack object is in the AppDelegate header. So the reference is strong.


I'm stuck here badly. what should i do?