//回顾一下 core data的用法
//以新建一个项目为例
1.新建项目 选择"Utility Application" -> 选择"Use Core Data"
2.点击proName.xcdatamodeld -> Add Entity 增加需要的实体(也就是java中的bean)
3.entity加完之后 选择工程 新建(command+n)
-> 选择"Core Data" 中的 "NSManagedObjectsubclass"
-> 勾选proName.xcdatamodeld
-> 勾选entity
-> create
4. AppDelegate.h 重要的三个属性变量
5. AppDelegate.m 中三个重要方法
(1)
6.纯属查询
//假设 有一个entity Department(deptID,deptName)
//同时 sqlite中已经有数据 我要从中取出所有的department然后放到一个数组中
(1) DeptViewController.h
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Department.h"
(2)
7. 删除数据 (先后经过"查询数据"、"删除数据"、"保存数据")
8. 添加数据
//以新建一个项目为例
1.新建项目 选择"Utility Application" -> 选择"Use Core Data"
2.点击proName.xcdatamodeld -> Add Entity 增加需要的实体(也就是java中的bean)
3.entity加完之后 选择工程 新建(command+n)
-> 选择"Core Data" 中的 "NSManagedObjectsubclass"
-> 勾选proName.xcdatamodeld
-> 勾选entity
-> create
4. AppDelegate.h 重要的三个属性变量
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
5. AppDelegate.m 中三个重要方法
(1)
- (NSManagedObjectContext *)managedObjectContext
{...}
- (NSManagedObjectModel *)managedObjectModel
{...}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{...}
6.纯属查询
//假设 有一个entity Department(deptID,deptName)
//同时 sqlite中已经有数据 我要从中取出所有的department然后放到一个数组中
(1) DeptViewController.h
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Department.h"
(2)
- (AppDelegate*)appDelegate{
return (AppDelegate*)[UIApplication sharedApplication].delegate;
}
- (void)loadDBData{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Doctors class])];
// sort by
NSSortDescriptor *sortID = [[NSSortDescriptor alloc] initWithKey:@"deptId" ascending:YES];
NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"deptName" ascending:YES];
NSArray *sortArr = [NSArray arrayWithObjects:sortID, sortName, nil];
[request setSortDescriptors:sortArr];
// where
NSPredicate *where = [NSPredicate predicateWithFormat:@"deptId == %@", @"0"];
[request setPredicate:where];
NSArray *resultArr = [[self appDelegate].managedObjectContext executeFetchRequest:request error:NULL]; //resultArr中的对象全部是Department类型
self.deptArray = resultArr;
[sortID release];
[sortName release];
[request release];
}
7. 删除数据 (先后经过"查询数据"、"删除数据"、"保存数据")
- (void)deleteTable:(Class)tablename{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *selectionItems = [NSEntityDescription entityForName:NSStringFromClass([tablename class])
inManagedObjectContext:[self appDelegate].managedObjectContext];
fetchRequest.entity = selectionItems;
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:[[self appDelegate].managedObjectContext
executeFetchRequest:fetchRequest
error:NULL]];
while([items count] > 0) {
id item = [items lastObject];
[[self appDelegate].managedObjectContext deleteObject:item];
[items removeObject:item];
}
[items release];
[fetchRequest release];
[[self appDelegate].managedObjectContext save:NULL];
}
8. 添加数据
- (void)insertInManagedObjectContext:(NSManagedObjectContext *)moc withData:(NSDictionary *)data{
Department *bean = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Department class])
inManagedObjectContext:moc];
bean.deptId = [data valueForKey:@"aId"];
bean.deptName = [data valueForKey:@"aName"];
[moc save:NULL];
}