coreData中的NSFetchedResultsController 用法

//回顾一下 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 重要的三个属性变量

@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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值