CoreData快速上手

#import "AppDelegate.h"
#import "UserInfo.h"
@interface AppDelegate ()
@property(strong,nonatomic)NSMutableArray *dataArr;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [self addUser];//添加
    [self gainDatas];//查询
    [self deleteUser:[_dataArr objectAtIndex:5]];//删除
    [self  updateUser:[_dataArr objectAtIndex:1]];//修改
    [self printAll];//查看全部
    return YES;
}
//添加
-(void)addUser
{
    for(int i = 0; i < 10; i++)
    {
        UserInfo *userInfo = [NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
        userInfo.name = [NSString stringWithFormat:@"name:%d",i];
        userInfo.password = [NSString stringWithFormat:@"password:10%d",i];
        NSError *error = nil;
        if(![self.managedObjectContext save:&error])
        {
            NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
            abort();
        }
        NSLog(@"add success");
    }
    
    
}

//查询
-(void)gainDatas
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext]];
    //查询条件
    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like [cd] 'baobao'"];
    //排序
    NSMutableArray *sortDescriptors = [NSMutableArray array];
    NSSortDescriptor *dec = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [sortDescriptors addObject:dec];
    //查询条件
    //[fetchRequest setPredicate:predicate];
    //分页
    [fetchRequest setFetchBatchSize:5];
    //排序
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    [fetchRequest setReturnsObjectsAsFaults:NO];
    //查询字段 一般不需要
    [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"name",@"password", nil]];
    NSError *error = nil;
    //通过上下文执行查询
    NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    if (fetchedItems == nil) {
        NSLog(@"fetch request resulted in an error %@,%@",error,[error userInfo]);
    }
    else
    {
        _dataArr = [NSMutableArray array];
        for(int i = 0 ; i < fetchedItems.count ;i ++)
        {
            UserInfo *user = [fetchedItems objectAtIndex:i];
            [_dataArr addObject:user];
        }
    }
}

//删除 NSManageedObject对象必须先取出来,用managedObjectedContext删除,然后保存
-(void)deleteUser:(id)user
{
    [self.managedObjectContext deleteObject:user];
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
        abort();
    }
    NSLog(@"delete success %@",_dataArr);
}

//更新 直接修改对象,保存manageObjectContext
-(void)updateUser:(UserInfo *)user
{
    user.name = @"baobao";
    NSError *error = nil;
    if(![self.managedObjectContext save:&error])
    {
        NSLog(@"Unresolved error:%@,%@",error,[error userInfo]);
        abort();
    }
    NSLog(@"update success");
    NSLog(@"%@",_dataArr);
}

//输出查看全部内容
-(void)printAll
{
    for (UserInfo *user in _dataArr) {
        NSLog(@"%@,%@",user.name,user.password);
    }
}

你可能感兴趣的:(CoreData快速上手)