---
title: "为CoreData添加版本管理"
author: "mrjeye"
date: "2012-03-30T10:32:19Z"
tags: []
categories: ["移动开发"]
slug: "coredata"
draft: false
lastmod: "2012-03-30T10:32:19Z"
id: 1469974
url: "https://mrjeye.iteye.com/blog/1469974"
---

```java

/**
 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:@"golf.sqlite"];
    
    NSError *error = nil;
    persistentStoreCoordinator_ = [[nspersistentstorecoordinator alloc] initWithManagedObjectModel:[self managedobjectmodel]];
    
    
    NSDictionary *options = [nsdictionary dictionarywithobjectsandkeys:
                             [nsnumber numberwithbool:yes], NSMigratePersistentStoresAutomaticallyOption,
                             [nsnumber numberwithbool:yes], NSInferMappingModelAutomaticallyOption, nil]; 
    
    if (![persistentstorecoordinator_ addpersistentstorewithtype:nssqlitestoretype
                                                   configuration:nil 
                                                             url:storeurl 
                                                         options:options 
                                                           error:&error]) {
        
        NSLog(@"Unresolved error %@, %@", error, [error userinfo]);
        abort();
    }    
    
    return persistentStoreCoordinator_;
}

```