Core Data的核心概念是托管对象。托管对象是由Core Data管理的简单模型对象,但必须是NSManagedObject类或其子类的实例。可以用一个称为托管对象模型的结构(schema)来描述 Core Data应用程序的托管对象(Xcode中包含一个数据建模工具,可以帮助您创建这些结构)。托管对象模型包含一些应用程序托管对象(也称为实体)的描 述。每个描述负责指定一个实体的属性、它与其它实体的关系、以及像实体名称和实体表示类这样的元数据。
通过Core Data管理应用程序的数据模型,可以极大程度减少需编写的代码数量。Core Data还具有下述特征:
- 将对象数据存储在SQLite数据库以获得性能优化。
- 提供NSFetchedResultsController 类用于管理表视图的数据。即将Core Data的持久化存储显示在表视图中,并对这些数据进行管理:增、删,改。
- 管理undo/redo操作。
- 检查托管对象的属性值是否正确。
接下来我们通过一个实例来学习一下Core Data 的基本用法:对数据的增删改查。
RootViewController 用来显示数据库中的数据,AddViewController用来向数据库中添加数据。
- 新建工程勾选Use Core Data选项,系统自动生成一些代码或后面自己新建xxx.xcdatamodeld;
- 打开xxx.xcdatamodeld文件新建实体,这里为Person;
- 为实体设置属性,及属性类型,这里为name,age;
- 然后选择新建文件->ios/osx 里的CoreData>NSManagedObjectsubclass,选择刚才的Person,自动生成Person.h和Person.m类。
理解appDelegate中生成的代码:
NSManagedObjectContext:可以理解成一个数据库,里面存着很多表。
NSManagedObjectModel:可以理解成对数据的描述,里面保存了实体类对象。
NSPersistentStoreCoordinator:可以理解成连接数据库时的一些操作,协调者。
一,插入操作;
1
|
-
(
void
)
saveData
{
//通信中心,通过此单例找到appDeleage中的数据库
AppDelegate
*appDelegate
=
[
UIApplication
sharedApplication
]
.
delegate
;
NSManagedObjectContext
*managedContext
=
appDelegate
.
managedObjectContext
;
//找到对应的textfield
UITextField
*name
=
(
UITextField
*
)
[
self
.
view
viewWithTag
:
100
]
;
UITextField
*age
=
(
UITextField
*
)
[
self
.
view
viewWithTag
:
200
]
;
//初始化实体类
Person *
per
=
[
NSEntityDescription
insertNewObjectForEntityForName
:
@"Person"
inManagedObjectContext
:managedContext
]
;
//为实体类赋值
per
.
name
=
name
.
text
;
per
.
age
=
[
NSNumber
numberWithInt
:
[
age
.
text
intValue
]
]
;
//保存实体的更改
[
appDelegate
saveContext
]
;//每次coredata的数据发生改变都必须调用改函数
}
|
二,查询操作;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//.h
//RootViewController
#pragma mark - View lifecycle
-
(
void
)
viewWillAppear
:
(
BOOL
)
animated
{
[
super
viewWillAppear
:animated
]
;
///指定数据库
AppDelegate
*appDelegate
=
[
UIApplication
sharedApplication
]
.
delegate
;
NSManagedObjectContext
*managedContext
=
appDelegate
.
managedObjectContext
;
//指定数据表
NSEntityDescription
*entity
=
[
NSEntityDescription
entityForName
:
@"Person"
inManagedObjectContext
:managedContext
]
;
//新建一个请求命令,相当于select
NSFetchRequest
*request
=
[
[
NSFetchRequest
alloc
]
init
]
;
//request 指定数据表
[
request
setEntity
:entity
]
;
//错误描述
NSError
*error
;
//接收查询出来的数据
NSArray
*arr
=
[
managedContext
executeFetchRequest
:request
error
:
&
error
]
;
//self.allData里存放的是Person对象获取里面的数据可以用per.age
self
.
allData
=
[
arr
mutableCopy
]
;
[
self
.
tableView
reloadData
]
;
}
|
三,删除操作;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
-
(
void
)
tableView
:
(
UITableView
*
)
tableView
commitEditingStyle
:
(
UITableViewCellEditingStyle
)
editingStyle
forRowAtIndexPath
:
(
NSIndexPath
*
)
indexPath
{
AppDelegate
*appdelegate
=
[
UIApplication
sharedApplication
]
.
delegate
;
Person
*per
=
[
self
.
allData
objectAtIndex
:indexPath
.
row
]
;
if
(
editingStyle
==
UITableViewCellEditingStyleDelete
)
{
//数组中删除
[
self
.
allData
removeObject
:per
]
;
//数据库中删除
[
appdelegate
.
managedObjectContext
deleteObject
:per
]
;
}
//保存更改
[
appdelegate
saveContext
]
;
//reload data
[
tableView
reloadData
]
;
}
|
四,修改操作;
1
2
3
4
5
6
7
8
9
10
11
|
//RootViewController
///
-
(
void
)
tableView
:
(
UITableView
*
)
tableView
didSelectRowAtIndexPath
:
(
NSIndexPath
*
)
indexPath
{
AppDelegate
*appDelegate
=
[
UIApplication
sharedApplication
]
.
delegate
;
//NSManagedObjectContext *context = appDelegate.managedObjectContext;
Person
*per
=
[
self
.
allData
objectAtIndex
:indexPath
.
row
]
;
per
.
name
=
@"Objective-c"
;
[
appDelegate
saveContext
]
;
[
tableView
reloadData
]
;
}
|
使用Core Data 持久化数据非常方便,减少代码量,并没有写Sql语句来操作数据库,简化操作,如果我们想查看底层的sql语句怎么做呢,编辑工程的Scheme>Arguments>>> -com.apple.CoreData.SQLDebug 1 再次运行程序你会发现控制台打印了底层的sql语句。