iOS CoreData(一)

一.CoreData主要的几个类

NSManagedObjectContext
托管对象上下文,进行数据操作时大多都是和这个类打交道。
NSManagedObjectModel
托管对象模型,一个托管对象模型关联一个模型文件(.xcdatamodeld),存储着数据库的数据结构。
NSPersistentStoreCoordinator
持久化存储协调器,负责协调存储区和上下文之间的关系。
NSManagedObject
托管对象类,所有CoreData中的托管对象都必须继承自当前类,根据实体创建托管对象类文件。

二.写一个manage管理类

头文件

@interface QBZCoreDataManage : NSObject
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;

+(instancetype)sharedInstance;

//增删改查
//增
- (id)insertObject:(NSString *)type;
//删
- (void)deleteObject:(NSString *)type predicate:(NSPredicate *)predicate;
//改
- (id)changeObject:(NSString *)type predicate:(NSPredicate *)predicate;
//查
- (id)searchObject:(NSString *)type predicate:(NSPredicate *)predicate;

//保存
- (void)saveContext;
@end

实现

+ (instancetype)sharedInstance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        [instance managedObjectContext];
    });
    return instance;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if(!managedObjectContext)
    {
        managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
    }
    return managedObjectContext;
}


- (NSManagedObjectModel *)managedObjectModel
{
    if(managedObjectModel == nil){
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"QBZCoreData" withExtension:@"momd"];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
        
    }
    return managedObjectModel;
}


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if(!persistentStoreCoordinator)
    {
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
        NSString *dataPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        dataPath = [dataPath stringByAppendingFormat:@"/%@.sqlite", @"QBZCoreData"];
        NSLog(@"%@",dataPath);
//        NSDictionary *options = {};
        [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataPath] options:nil error:nil];
    }
    return persistentStoreCoordinator;
}


- (id)insertObject:(NSString *)type
{
    id some = [NSEntityDescription insertNewObjectForEntityForName:type inManagedObjectContext:[QBZCoreDataManage sharedInstance].managedObjectContext];
    return some;

}
- (void)deleteObject:(NSString *)type predicate:(NSPredicate *)predicate
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:type];
    request.predicate = predicate;
    NSError *error;
    NSArray *reqestArray = [[QBZCoreDataManage sharedInstance].managedObjectContext executeFetchRequest:request error:&error];
    [reqestArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[QBZCoreDataManage sharedInstance].managedObjectContext deleteObject:obj];
    }];
}

//改
- (id)changeObject:(NSString *)type predicate:(NSPredicate *)predicate
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:type];
    request.predicate = predicate;
    NSError *error;
    NSArray *resultArray = [[QBZCoreDataManage sharedInstance].managedObjectContext executeFetchRequest:request error:&error];
    if(resultArray.count>0){
        return resultArray[0];
    }
    return nil;
}
//查
- (id)searchObject:(NSString *)type predicate:(NSPredicate *)predicate
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:type];
    request.predicate = predicate;
    NSError *error;
    NSArray *resultArray = [[QBZCoreDataManage sharedInstance].managedObjectContext executeFetchRequest:request error:&error];
    return resultArray;

}

- (void)saveContext
{
    NSError *error;
    if ([QBZCoreDataManage sharedInstance].managedObjectContext.hasChanges) {
        [[QBZCoreDataManage sharedInstance].managedObjectContext save:&error];
    }
    
    if(error)
    {
        NSLog(@"CoreData save error:%@",error);
    }

}
@end

其他方法见demo

终端调试

iOS CoreData(一)_第1张图片
配置

iOS CoreData(一)_第2张图片
终端

你可能感兴趣的:(iOS CoreData(一))