2011-11-28 75 views
2

我想将CoreData添加到在Xcode 4.2中创建的现有iOS应用程序。因为它是基于标签栏的应用程序,所以我没有在创建时默认包含它的选项。将CoreData添加到现有的iOS项目 - 无法获得NSManagedObjectModel

到目前为止,我:

  • 添加了CoreData.framework到应用程序(在建目标确认)
  • 增加了一个模式“model.coredatamodeld”(在建目标确认,证实“妈妈”文件包含在应用程序目录中的iOS模拟器运行时)
  • 从新鲜CoreData自动生成的版本替换应用程序的委托功能的应用程式

我结束对象模型创建返回nil。如果我看看生成的URL,它是明智的,并指向我的文件系统中的“妈妈”文件夹。该文件夹包含'model.momd'和'VersionInfo.plist'。这两个文件都有明显的内容(前者是一些二进制数据,后者是一些合理的声音XML)。

__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL]; 
// __managedObjectModel is nil here 

如何调试?

下面是应用程序委托的源代码。

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

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    id ctx = self.managedObjectContext; 
    //id model = [self managedObjectModel]; 
    //NSLog(@"%@ %@", ctx, model); 
    return YES; 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Saves changes in the application's managed object context before the application terminates. 
    [self saveContext]; 
} 

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    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; 
    } 

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

    NSError *error = nil; 
    NSData *objectIDData = [NSData dataWithContentsOfURL:modelURL options:NSDataReadingMapped error:&error]; 
    NSLog(@"%@",objectIDData); 

    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:@"model.sqlite"]; 

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. 

     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: 
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

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

    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 

回答

1

我浪费在这个足够的时间和简单地创建了所需的“基本功能”(核心数据,故事情节,ARC等)的新项目,然后放弃我的代码到它。不是最好的解决方案,但它只需要大约10分钟,而且更重要的是,它的工作。

+0

我有这个相同的确切问题,除了它是与版本化的数据模型。一些关于配置的东西似乎变得清晰起来。我最终复制了我的模型文件(原始版本,在我的情况下),并将其粘贴到桌面上。然后我删除了我的所有datamodel文件(来自svn和文件系统),并将香草数据模型添加回我的项目。现在一切正常。只有调试受到影响...档案/分发没有任何问题。 – pinkeerach