2012-03-08 74 views
1

我已经尝试了很多不同的答案,但我似乎无法得到此工作。我正在尝试将Core Data添加到现有的基于Tab的项目中。我通过目标添加了核心数据框架,我正确设置了DataModel和实体,但似乎无法访问它。我已经得到了很多不同的错误,但最近的是:如何将CoreData添加到现有基于Tab的IOS项目

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model' 

我成立了一个新的公用基础的项目使用预设的核心数据,并作为直接复制代码。我只是将文件URL名称更改为当前项目,并且不起作用。这里是我的代码:

appDelegate.h

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
BOOL isNotFirstTime; 
NSMutableArray *teamMembers; 
NSMutableArray *projects; 
NSMutableArray *tasks; 


} 
@property(readwrite, retain) NSMutableArray *teamMembers; 
@property(readwrite, retain) NSMutableArray *projects; 
@property(nonatomic, retain) NSMutableArray *tasks; 
@property(nonatomic)BOOL isNotFirstTime; 
@property (strong, nonatomic) UIWindow *window; 

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

- (void)saveContext; 
- (NSURL *)applicationDocumentsDirectory; 
- (NSURL *)applicationDocumentsDirectory; 
- (void)saveContext; 

@end 

AppDelegate.m 出于某种原因,当我创建它保持为空文件URL .....我不知道为什么..

#import "AppDelegate.h" 
    #import "Task.h" 
    #import "Project.h" 
    #import "TeamMember.h" 
    #import "newTeamMemberWindow.h" 


    @implementation AppDelegate 

    @synthesize window = _window; 
    @synthesize tasks; 
    @synthesize teamMembers; 
    @synthesize projects; 
@synthesize isNotFirstTime; 
@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 


    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (!context) { 
     NSLog(@"No Context on App Load"); 
    } 

    newTeamMemberWindow *newTeamMemberWindowObject = [[newTeamMemberWindow alloc]init]; 
    newTeamMemberWindowObject.managedObjectContext = context; 


     return YES; 

    } 

//删除了所有正常的方法来巩固堆栈溢出

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

此代码分手的modelURL保持为空.....

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TimeLines" withExtension:@"momd"]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    NSLog(@"Created managedObjectModel with Url: %@", 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:@"TimeLines.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 
+0

这一切都源于您的modelURL没有设置。你的数据模型文件叫什么?检查区分大小写等,如果它是你唯一的模型,考虑mergedModelFromBundles:而不是派生一个URL。 – jrturton 2012-03-08 07:05:18

回答