存储上下文中的CoreData错误,NSPersistentStoreCoordinator没有持久存储

时间:2013-04-16 00:45:54

标签: iphone ios objective-c core-data

我看过我能找到的每一个类似的问题,他们的解决方案都没有为我工作。

一个问题可能是在我从JAP返回并从webAPI解析后,在上下文中添加了实体(成功)并且保存了上下文。但是第一次使用manageContext和persistent store时会设置上下文,如下所示。所以在解析之后它会在那个线程上发生。

确切错误:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

我尝试从模拟器中删除应用程序,因为建议不会更改。

这是我正在使用的CoreDataHelper类,我的应用程序中只有一个实例,当我在上下文中添加新项目后在助手上调用saveContext方法时会发生错误:

@implementation CoreDataHelper

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _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];
}

- (void)saveContext
{
    NSError *error = nil;
    if (self.managedObjectContext != nil) {
        if ([self.managedObjectContext hasChanges] && ![self.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();
        }
    }
}

- (void)dealloc {

    [self saveContext];
}

#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;
    }

    NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    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:@"Model" 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) {
        return _persistentStoreCoordinator;
    }

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

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                   initWithManagedObjectModel:[self managedObjectModel]];

    // needed for lightweight migrations
    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setObject:[NSNumber numberWithBool:YES]
                forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES]
                forKey:NSInferMappingModelAutomaticallyOption];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                         options:options
                                                           error:&error])  {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[error localizedDescription]
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil, nil];
        [alertView show];
    }

    return _persistentStoreCoordinator;
}


@end

ETA:我已经看过它了,但我如何/在哪里删除存储sql文件的机器上的本地文件夹?

2 个答案:

答案 0 :(得分:3)

我没有明确的答案,但这里有几点需要注意:

  1. 在您创建persistentStoreCoordinator的位置添加一些记录。如果在创建期间添加商店时发生错误,您可以在那里捕获它。

  2. 您提到要在后台线程上保存托管对象上下文。 NSManagedObjectContext不是线程安全的,您应该只在它创建的线程上使用特定的上下文。每个线程至少需要一个上下文。您可以观察“NSManagedObjectContextDidSaveNotification”并将更改合并到主要上下文中。

答案 1 :(得分:0)

原来我正在删除商店,而不是每次召回Web API时都不重新创建它。我最初的目标是每次用户想要通过调用api来刷新视频列表时清理数据库,然后删除所有对象,然后删除存储并忘记重新创建它。这是我的刷新数据库方案,我只是删除了saveContext调用之下的所有内容。

- (void)resetDataBase {

    [self deleteAllObjects:@"Speaker"];
    [self deleteAllObjects:@"Tag"];
    [self deleteAllObjects:@"UserGroup"];
    [self deleteAllObjects:@"Video"];

    [_coreDataHelper saveContext];

    // REMOVED BELOW and it worked
    NSPersistentStore * store = [[_coreDataHelper.persistentStoreCoordinator persistentStores] lastObject];
    if (store) {
        NSError * error;
        [_coreDataHelper.persistentStoreCoordinator removePersistentStore:store error:&error];
        if (error) {
            [self showError:error];
        }
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"UserGroupTV.sqlite"];
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
}