https://github.com/YouXianMing

使用CoreData [4]

使用CoreData [4]

此片文章主要是分析如何对CoreData进行封装.

在开始之前,我们需要弄明白3个非常关键的类,以下翻译凑合着看看.

 

NSManagedObjectContext

An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application. Its primary responsibility is to manage a collection of managed objects. These objects form a group of related model objects that represent an internally consistent view of one or more persistent stores. A single managed object instance exists in one and only one context, but multiple copies of an object can exist in different contexts. Thus object uniquing is scoped to a particular context.

一个NSManagedObjectContext代表着一个独立的对象空间或者是应用的一个记录暂存区.它主要的职责就是用来管理一系列的对象.这些对象来自于一系列的与model对象相关的组,用来代表一个存储器或者多个存储器.一个管理对象的实例只能有一个上下文,但是这个对象的多份拷贝能够存在不同的上下文.这些对象在特殊的上下文之间有着作用域的不同.

 

NSManagedObjectModel

An NSManagedObjectModel object describes a schema—a collection of entities (data models) that you use in your application.
一个NSManagedObjectModel对象描述了一种表结构-一个你的应用程序中实体类集合的描述.
The model contains one or more NSEntityDescription objects representing the entities in the schema. Each NSEntityDescription object has property description objects (instances of subclasses of NSPropertyDescription) that represent the properties (or fields) of the entity in the schema. The Core Data framework uses this description in several ways:
这个model包含了一个或者多个NSEntityDescription对象用来代表这个表中的实体类.每一个NSEntityDescription对象都有一个描述的对象,用来代表这个对象在表结构中的实体.Core Data 框架通过以下几种方式来使用这些描述信息:
Constraining UI creation in Interface Builder

在IB中创建UI
Validating attribute and relationship values at runtime

在运行时期间确认属性以及关系
Mapping between your managed objects and a database or file-based schema for object persistence.

在你管理的对像和数据库中的一张表产生映射关系.

NSPersistentStoreCoordinator

Instances of NSPersistentStoreCoordinator associate persistent stores (by type) with a model (or more accurately, a configuration of a model) and serve to mediate between the persistent store or stores and the managed object context or contexts. Instances of NSManagedObjectContext use a coordinator to save object graphs to persistent storage and to retrieve model information. A context without a coordinator is not fully functional as it cannot access a model except through a coordinator. The coordinator is designed to present a façade to the managed object contexts such that a group of persistent stores appears as an aggregate store. A managed object context can then create an object graph based on the union of all the data stores the coordinator covers.

NSPersistentStoreCoordinator的实体对象维系数据库,以及在数据库和对象之间,操作句柄之间进行着通信.NSPersistentStoreCoordinator的实例对象通过协调的方式来将对象本地持久化以及将这个对象从本地中读取出来.
Coordinators do the opposite of providing for concurrency—€”they serialize operations. If you want to use multiple threads for different write operations you use multiple coordinators. Note that if multiple threads work directly with a coordinator, they need to lock and unlock it explicitly.
Coordinators按照序列化操作.如果你想在多线程中使用不同的写操作,你需要使用不同的coordinator.请注意,如果多线程在操作一个coordinator,这些线程就需要通过加锁解锁的方式来保证操作的正确性.
Each coordinator (and thus container) may use different copies, and hence different versions, of a managed object model. This allows you to cleanly deal with file versioning.
每一个coordinator也许会用到不同的拷贝,以及继承不同的版本,这允许你兼容不同的版本.
The coordinator gives access to its underlying object stores. You can retrieve an object store when you first add one (using addPersistentStoreWithType:configuration:URL:options:error:), or by using persistentStoreForURL: or persistentStores. This allows you to to determine, for example, whether a store has already been added, or whether two objects come from the same store.
这个coordinator可以很便利的操作实时的存储.你可以立马就查出一个对象当你第一次添加了coordinator.

我们重新创建一个工程:

创建出CoreData

然后创建出Student类

然后,导入以下几个本人写的文件:

StoreCoordinator.h   +   StoreCoordinator.m

//
//  StoreCoordinator.h
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StoreCoordinator : NSObject

// 配置表文件以及数据库文件
+ (void)setBundleObjectModel:(NSString *)name setDBPath:(NSString *)path;

// 提供coordinator
+ (id)coordinator;

@end
//
//  StoreCoordinator.m
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

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

static NSPersistentStoreCoordinator *storeCoordinator   = nil;

@implementation StoreCoordinator

+ (void)setBundleObjectModel:(NSString *)name setDBPath:(NSString *)path
{
    if (storeCoordinator == nil)
    {
        // 获取bundle中的表结构
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:name
                                                  withExtension:@"momd"];
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        
        // 获取表结构
        storeCoordinator = \
            [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        // 由表结构与数据库建立联系
        [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                       configuration:nil
                                                 URL:[NSURL fileURLWithPath:path]
                                             options:nil
                                               error:nil];
    }
}

+ (id)coordinator
{
    return storeCoordinator;
}

@end

ObjectContext.h  +  ObjectContext.m

//
//  ObjectContext.h
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ObjectContext : NSObject

@property (nonatomic, strong, readonly) NSManagedObjectContext *context;

// 从coordinator中初始化一个操作句柄
- (instancetype)initWithCoordinator:(id)coordinator;

// 保存这个操作句柄中的所有变化
- (void)saveChanges;

@end
//
//  ObjectContext.m
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

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

@interface ObjectContext ()

@property (nonatomic, strong) NSManagedObjectContext *context;

@end

@implementation ObjectContext

- (instancetype)initWithCoordinator:(id)coordinator
{
    self = [super init];
    if (self)
    {
        _context = [[NSManagedObjectContext alloc] init];
        [_context setPersistentStoreCoordinator:coordinator];
    }
    return self;
}

- (void)saveChanges
{
    NSError *error = nil;
    if (_context != nil)
    {
        if ([_context hasChanges] && ![_context save:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

@end

NSManagedObject+YX.h   +   NSManagedObject+YX.m

//
//  NSManagedObject+YX.h
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <CoreData/CoreData.h>
@class ObjectContext;

@interface NSManagedObject (YX)

// 创建一条记录
+ (id)createInContext:(ObjectContext *)context;

// 查询所有记录
+ (NSArray *)allInContext:(ObjectContext *)context;

// 删除一条记录
- (void)deleteSelf;

@end
//
//  NSManagedObject+YX.m
//  http://home.cnblogs.com/u/YouXianMing/
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "NSManagedObject+YX.h"
#import "ObjectContext.h"

@implementation NSManagedObject (YX)

+ (id)createInContext:(ObjectContext *)context
{
    return [NSEntityDescription insertNewObjectForEntityForName:[self entityName]
                                         inManagedObjectContext:context.context];
}

+ (NSArray *)allInContext:(ObjectContext *)context
{
    // 设定要查询的实体
    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:[self entityName]];
    
    // 取出查询结果
    return [context.context executeFetchRequest:fetch error:nil];
}

+ (NSString *)entityName
{
    return NSStringFromClass(self);
}

- (void)deleteSelf
{
    [self.managedObjectContext deleteObject:self];
}

@end

然后在AppDelegate中粘贴以下代码并运行一次后.

//
//  AppDelegate.m
//  StudyCoreData
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "AppDelegate.h"

#import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h"

#import "Student.h"

@interface AppDelegate ()

@property (nonatomic, strong) ObjectContext *context;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"%@", NSHomeDirectory());
    
    // 配置CoreData
    [StoreCoordinator setBundleObjectModel:@"YouXianMing"
                                 setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]];
    
    // 从coordinator获取到操作句柄
    _context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]];
    
    // 存储变化
    [_context saveChanges];
    
    return YES;
}

- (NSString *)path:(NSString *)filePath
{
    return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
}

@end

配置CoreData其实就是以下含义.

然后运行以下代码后查看执行结果.

//
//  AppDelegate.m
//  StudyCoreData
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "AppDelegate.h"

#import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h"

#import "Student.h"

@interface AppDelegate ()

@property (nonatomic, strong) ObjectContext *context;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 配置CoreData
    [StoreCoordinator setBundleObjectModel:@"YouXianMing"
                                 setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]];
    
    // 从coordinator获取到操作句柄
    _context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]];
    
    // 创建学生对象
    Student *stu1 = [Student createInContext:_context];
    Student *stu2 = [Student createInContext:_context];
    stu1.name     = @"YouXianMing";
    stu2.name     = @"QiuLiang";
    
    // 存储变化
    [_context saveChanges];
    
    // 遍历出所有学生
    NSArray *allStudents = [Student allInContext:_context];
    for (Student *stu in allStudents)
    {
        NSLog(@"%@", stu.name);
    }
    
    return YES;
}

- (NSString *)path:(NSString *)filePath
{
    return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
}

@end

以上就已经完成了CoreData记录的创建以及记录的遍历.

我们在试一下删除掉一个记录吧.

运行以下代码后查看执行结果.

//
//  AppDelegate.m
//  StudyCoreData
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "AppDelegate.h"

#import "StoreCoordinator.h"
#import "ObjectContext.h"
#import "NSManagedObject+YX.h"

#import "Student.h"

@interface AppDelegate ()

@property (nonatomic, strong) ObjectContext *context;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 配置CoreData
    [StoreCoordinator setBundleObjectModel:@"YouXianMing"
                                 setDBPath:[self path:@"/Library/Caches/Y.Y.M.sqlite"]];
    
    // 从coordinator获取到操作句柄
    _context = [[ObjectContext alloc] initWithCoordinator:[StoreCoordinator coordinator]];
    
    // 遍历出所有学生
    NSArray *allStudents = [Student allInContext:_context];
    for (Student *stu in allStudents)
    {
        if ([stu.name isEqualToString:@"QiuLiang"])
        {
            // 删除掉对象
            [stu deleteSelf];
        }
    }
    
    // 存储变化
    [_context saveChanges];
    
    // 遍历出所有学生
    NSArray *all = [Student allInContext:_context];
    for (Student *stu in all)
    {
        NSLog(@"%@", stu.name);
    }
    
    return YES;
}

- (NSString *)path:(NSString *)filePath
{
    return [NSHomeDirectory() stringByAppendingPathComponent:filePath];
}

@end

查看下图,记录被删除了哦.

用起来是不是很容易呢:)

关于CoreData的查询语句,这个就需要看官自己根据需要修改我的源码了,本人只是抛砖引玉介绍CoreData的基本使用方法,剩下的就靠大家了哦.

 

posted @ 2014-07-31 16:41 YouXianMing 阅读(...) 评论(...) 编辑 收藏