论坛首页 移动开发技术论坛

添加 core data 支持 ios 项目

浏览 6246 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-26   最后修改:2011-04-26
iOS

网上很多教程包括apple自己的都是从一个core data的模板项目教起, 没有一个怎么在既有的项目上添加core data的方法(也许有,反正我没找到), 下面就是自己研究的添加core data的步骤, 我用的是mac os x 10.6.7, xcode4.0.0

 

 

1.  添加core data framework 

xcode中选中项目根,在右边的面板中选中targets下面的项目名,再在右边选中 Build Phases, 再选中Link Binary with Libraries,  点加号添加core data 框架

 

   在项目中找一个xx.pch的文件, 加上一行      #import <CoreData/CoreData.h>

 

2. 在项目的XXXAppDelegate.h 文件中 加入

 

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


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

XXXAppDelegate.m文件中加入

 

 

@synthesize managedObjectContext=__managedObjectContext;//session

@synthesize managedObjectModel=__managedObjectModel;

@synthesize persistentStoreCoordinator=__persistentStoreCoordinator;



//相当与持久化方法
- (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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            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.
 */
//初始化context对象
- (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;
    }
//这里的URLForResource:@"lich" 的url名字(lich)要和你建立datamodel时候取的名字是一样的,至于怎么建datamodel很多教程讲的很清楚
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"lich" 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;
    }
    //这个地方的lich.sqlite名字没有限制,就是一个数据库文件的名字
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"lich.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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         
         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];
}

 

因为加入了新的属性,自己记得去release

 

这里比较奇怪的一点是RLForResource:@"lich" withExtension:@"momd"]; 扩展名是momd 而不是xcdatamodeld,不明白

 

上面很多代码是从core data模板项目里copy过来的,大致的意思就是先建立一个managedObjectModel,

然后根据 managedObjectModel 建立 persistentStoreCoordinator

然后根据 persistentStoreCoordinator 建立 managedObjectContext

 

3. 然后是把managedObjectContext传给真正要使用数据的 viewcontroller

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // NSLog(@"%@===================", [self applicationDocumentsDirectory]);
    [self.window makeKeyAndVisible];
    NSManagedObjectContext *context = [self managedObjectContext];
    NSArray *array =  self.tabBarController.viewControllers;
    for (UINavigationController *navi in array) {
        RootView *rv = (RootView *)navi.topViewController;
        rv.context = context;
    }
 

 

 

我开始不明白为什么要在XXXAppDelegate中建立managedObjectContext 而不是在viewcontroller中,那样就不用传来传去了啊

也许是view对象有时候会被销毁吧

 

 

至于其他的怎么插数据 读数据, apple教程上都有就不讲了。

 

 

 

 

 

 

 

 

 

   发表时间:2011-05-30  
谢谢lz,补充一点

引用
这里比较奇怪的一点是RLForResource:@"lich" withExtension:@"momd"]; 扩展名是momd  而不是xcdatamodeld,不明白


xcdatamodeld文件是编译前的状态,这个文件在编译时会被转为momd文件被拷贝到App的main bundle里
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics