【问题标题】:IOS CoreData - Cant load NSPersistentStoreCoordinatorIOS CoreData - 无法加载 NSPersistentStoreCoordinator
【发布时间】:2011-04-16 11:18:02
【问题描述】:

我有一个名为 TestProject 的 xcode4 项目,我正在尝试将 CoreData 添加到其中。我添加了一个包含一些实体的数据模型(名为 TestDataModel),并为这些实体创建了 NSManagedObject 类。

我的问题是我无法加载持久存储

if(self.managedObjectContext == nil){
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        NSURL* storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingPathComponent: @"TestProject.sqlite"]];
        NSError* errors;

        NSPersistentStoreCoordinator* persistentStoreCoordinator = 
                           [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel 
                                                                     mergedModelFromBundles:nil ]];

        if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                     configuration:nil 
                                                               URL:storeUrl 
                                                           options:nil 
                                                             error:&errors])
        {
            NSLog(@"Error loading persistent store: %@", [errors localizedDescription]);
        }

        self.managedObjectContext = [[NSManagedObjectContext alloc] init];
        [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];

    }

我不断收到错误:

Error loading persistant store: The operation couldn’t be completed. (Cocoa error 258.)

请注意,我也在模拟器中运行它。

提前致谢。

【问题讨论】:

    标签: ios core-data


    【解决方案1】:

    你使用了错误的目录。

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^
    

    改成

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                                                         ^^^^^^^^^^^^^^^^^^^
    

    NSDocumentDirectory 是由 iOS 创建的,但 NSDocumentationDirectory 目录不是。

    【讨论】:

      【解决方案2】:

      想通了。没有意识到我需要创建数据库

      以下代码有效

      if(self.managedObjectContext == nil){
      
              NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"TestProject.sqlite"];
      
              NSFileManager *fileManager = [NSFileManager defaultManager];
              if (![fileManager fileExistsAtPath:storePath]) {
                  NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"TestProject" ofType:@"sqlite"];
                  if (defaultStorePath) {
                      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
                  }
              }
      
              NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
      
      
              NSError* errors;
              NSPersistentStoreCoordinator* persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
      
              if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&errors])
              {
                  NSLog(@"Error loading persistent store: %@", [errors localizedDescription]);
              }
      
              self.managedObjectContext = [[NSManagedObjectContext alloc] init];
              [self.managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
      
          }
      

      你也需要这个

      - (NSString *)applicationDocumentsDirectory {
          return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        • 2015-01-29
        • 2020-05-30
        • 2012-08-18
        • 2019-11-16
        • 2014-12-24
        相关资源
        最近更新 更多