Frage

I've been working all day to solve an error that I've found to be an NSInternalInconsistencyException error. The error console message I get is "This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation." Also I get "Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x1)."

I have the following code in my App Delegate:

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

#pragma mark Core Data stack

- (NSManagedObjectContext *)managedObjectContext {

    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return _managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel {

    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"shindy.sqlite"];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}


#pragma mark Application's documents directory

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

I have throughly read up on other questions similar to this one, and it appears that my code is consistent, and I shouldn't be having this problem. And yet, I am...

I have tried both cleaning & deleting the app and reinstalling. I tried the app on both a device & on the simulator. I changed the name of the appended URLPath, "shindy.sqlite", to various names I haven't used before. I also tried forcefully instantiating [self saveContext]; in my applicationDidFinishLaunching method in an attempt to make my app create the store.

All of my attempts to solve the problem have simply returned the same error. I don't know what else to try.

War es hilfreich?

Lösung

I eventually figured out what I was doing wrong. Unfortunately, it probably won't be of much help to most of you as it was a silly error that I made.

I was doing the normal "appending" onto the URL in the App Delegate. And then "appending" again in the first view controller that would load. This resulted in my code appending a name onto an already created sqlite folder.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top