zoukankan      html  css  js  c++  java
  • CoreData数据库浅析

     Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象。在此数据操作期间,我们不需要编写任何SQL语句。简单地用下图描述下它的作用:

    abc

    模型文件

      在Core Data,需要进行映射的对象称为实体(entity),而且需要使用Core Data的模型文件来描述app中的所有实体和实体属性。这里以Person(人)和Card(身份证)2个实体为例子,先看看实体属性和实体之间的关联关系:
    abc
    Person实体中有:name(姓名)、age(年龄)、card(身份证)三个属性
    Card实体中有:no(号码)、person(人)两个属性

    初步建立一个coreData,进行简单的数据的增删改查。

    1、新建工程,记得勾选Use Core Data

    2、建立好以后可以看到xxx.xcdatamodeld,在这里可以添加实体和实体的属性。需要注意的是:实体名字必须以大写开头。

    3、然后新建一个file,记得是NSManagedObject cubclass

    4、勾选自己建立的工程

    5、勾选建立的实体

    6、next以后我们就可以看到建立好的实体是有4个文件,如图一

    这里需要注意的是,xcode7以后建立的都是4个,而7以前的是两个,如图二

    解释如下:So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

    In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!

    图一


    图二

    7、

    8、增删改查(其实顺序应该是增、查、删或者改)

    查询结果

    删除后再进行查,查询结果:

    改完后的结果:

     
     

     

  • 相关阅读:
    kafka重新启动时出现:found a corrupted index file due to requirement failed问题解决方法
    filebeat向kafka中传输数据报WARN Failed to connect to broker DOMSDev07:9092: dial tcp: lookup DOMSDev07: getaddrinfow: No such host is known.解决方法
    安装Zookeeper出现Unable to start AdminServer,existing abnormally问题解决方法
    Windows Server 2008 R2 安装WinDbg以及符号路径设置
    Windows安装ElastAlert问题总结
    Windows系统下Log4Net+FileBeat+ELK日志分析系统问题总结
    Anaconda和basemap的安装和使用
    temp
    Mininet-wifi典型用法
    mininet-wifi 创建ryu和ofsoftswitch的拓扑流程
  • 原文地址:https://www.cnblogs.com/iOSlearner/p/5476761.html
Copyright © 2011-2022 走看看