zoukankan      html  css  js  c++  java
  • coreData

    请尊重原创的劳动成果:罗朝辉(http://www.cppblog.com/kesalin)

    http://www.cppblog.com/ipzyh/articles/CoreData.html

    Entity - NSEntityDescription

    Entity 相当于数据库中的一个表,它描述一种抽象数据类型,其对应的类为 NSManagedObject 或其子类。

    NSEntityDescription 常用方法:
    +insertNewObjectForEntityForName:inManagedObjectContext: 工厂方法,根据给定的 Entity 描述,生成相应的 NSManagedObject 对象,并插入 ManagedObjectContext 中。
    -managedObjectClassName 返回映射到 Entity 的 NSManagedObject 类名
    -attributesByName 以名字为 key, 返回 Entity 中对应的 Attributes
    -relationshipsByName 以名字为 key, 返回 Entity 中对应的 Relationships

    Property - NSPropertyDescription

    2)Property - NSPropertyDescription
    Property 为 Entity 的特性,它相当于数据库表中的一列,或者 XML 文件中的 value-key 对中的 key。它可以描述实体数据(Attribute),Entity之间的关系(RelationShip),或查询属性(Fetched Property)。

     > Attribute - NSAttributeDescription
    Attribute 存储基本数据,如 NSString, NSNumber or NSDate 等。它可以有默认值,也可以使用正则表达式或其他条件对其值进行限定。一个属性可以是 optional 的。
     
     > Relationship - NSRelationshipDescription 
    Relationship 描述 Entity,Property 之间的关系,可以是一对一,也可以是一对多的关系。 

     > Fetched Property - NSFetchedPropertyDescription
    Fetched Property 根据查询谓词返回指定 Entity 的符合条件的数据对象。

    1. CoreData:单表

    需要导入CoreData框架

    1> 创建模型文件 [相当于一个数据库里的表]NSManagedObjectModel

    2> 添加实体 [一张表]Add Entity

    3> 创建实体类 [相当模型]

    4> 生成上下文 关联模型文件生成数据库 NSManagedObjectContext

    关联的时候,如果本地没有数据库文件,CoreData自己会创建

    2. 创建:

    持久化,把数据保存到一个文件,而不是内存

    // 上下文关连数据库
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    
    // model模型文件
        NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
        
    // 持久化存储调度器
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        // 告诉Coredata数据库的名字和路径
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
        NSLog(@"%@",sqlitePath);
        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
        
        context.persistentStoreCoordinator = store;
    // 切记要保存 !!!
        _context = context;

    3. 增加数据:

    插入数据:用描述NSEntityDescription

    Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
    [_context save:&error];

    4. 查询数据:

    只要涉及到查询数据的都要用:NSFetchRequest(修改数据 删除数据 查询数据都需要用)谓词:NSPredicate

    // 1.FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
        
    // 2.设置过滤条件
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",
                            @"zhangsan"];
        //request.predicate = pre;
        
    // 3.设置排序
        NSSortDescriptor *heigtSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
        request.sortDescriptors = @[heigtSort];
        
    // 4.执行请求
        NSError *error = nil;
        
        NSArray *emps = [_context executeFetchRequest:request error:&error];
        if (error) {
            NSLog(@"error");
        }

    在对数据库中的数据进行删除和修改操作之后需要保存数据:

    [_context save:nil];

    知识点:

    数据存储的结构比较简单的时候,使用CoreData

    开发效率会高点,为什么?面向对象,而且不用写sql语句

    FMDatabases 数据结果比较复杂的时候,表与表之前的关联比较的时候

    5. CoreData多表:

    多表的重点就是在单表的基础上增加了关联

    例如:Student表和Teacher表  Student对Teacher:多对一   To One(反之:To Many)

    设置关联之后可以关系名获取对方的信息

    6. CoreData查询:

    1> 分页查询:

    fetchOffset:分页查询的起始索引

    fetchLimit:每次查询数据的条数

    fetchBatchSize:一次加载到内存的条数

    2> 模糊查询:

    模糊查询就是通过设置过滤条件来实现:

    过滤条件:NSPredicate:

    CONTAINS   LIKE  BEGINSWITH ENDSWITH

    7. CoreData多个数据库:

    7.1 一个数据库对应一个上下文

    _companyContext = [self setupContextWithModelName:@"Company"];
        _weiboContext = [self setupContextWithModelName:@"Weibo"];

    使用下面的方法,如果 bundles为nil 会把bundles里面的所有模型文件的表放在一个数据库(把所有的表放在一个数据库中)

    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

    要想一个数据库中存储一个模型文件 需要如下代码:

    //  URLForResource:每个模型文件的名字
    NSURL *companyURL = [[NSBundle mainBundle] URLForResource:@"Company" withExtension:@"momd"];
    // 创建NSManagedObjectModel需要NSURL
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:companyURL];

    7.2 如何开启CoreData的SQL语句输出开关:

    点击Product--EditScheme   -- Arguments -- ArgumentsPassed On Launch 添加:

    1>    -com.apple.CoreData.SQLDebug

    2>     1

    8. 网络:

    OSI(Open System Interconnection)开放系统应用互联 参考模型:

    从上至下:

    应用层(APDU) -- 表示层(PPDU) -- 会话层(SPDU) -- 传输层(TPDU) -- 网络层(报文)-- 数据链路层(帧) -- 物理层(比特)

  • 相关阅读:
    中国历史朝代公元对照简表
    [Solved] DashBoard – Excel Service: The data sources may be unreachable, may not be responding, or may have denied you access.
    Delete/Remove Project from TFS 2010
    Sharepoint site showing system account instead of my username on the top right corner.
    你的成功在于你每天养成的习惯
    Internet Information Services is running in 32bit emulation mode. Correct the issue listed above and rerun setup.
    Prepare to back up and restore a farm (Office SharePoint Server 2007)
    Word中字号与磅值的对应关系
    How to: Change the Frequency for Refreshing the Data Warehouse for Team System
    UI Automation in WPF/Silverlight
  • 原文地址:https://www.cnblogs.com/Evelyn-zn/p/4989996.html
Copyright © 2011-2022 走看看