标签:
关于CoreData的介绍太多,网上一搜大把全是,这里不介绍CoreData,直接上代码,注释写的很详细,应该很容易理解,暂时现做简单的增删该查,后面有时间再做修改完善。
CoreDataManager.h
1 // 2 // CoreDataManager.h 3 // UCaiYuan 4 // 5 // Created by wanyakun on 16/1/6. 6 // Copyright © 2016年 com.ucaiyuan. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface CoreDataManager : NSObject 12 13 +(CoreDataManager *)defaultManager; 14 15 /** 16 * 插入单条数据到Entity 17 * 18 * @param entity 实体字典 19 * @param name Entity名称 20 */ 21 - (BOOL)insertCoreDataWithEntity:(NSDictionary *)entity name:(NSString *)name; 22 23 /** 24 * 根据查询条件从实体中查询数据 25 * 26 * @param entityName Entity名称 27 * @param predicate 查询条件 28 * 29 * @return 字典数组 30 */ 31 - (NSMutableArray *)selectCoreDataFromName:(NSString *)name predicate:(NSString *)predicate; 32 33 /** 34 * 更新单条数据到Entity 35 * 36 * @param entity 更新内容 37 * @param name Entity名字 38 * @param predicate 更新条件 39 * 40 * @return 是否更新成功 41 */ 42 - (BOOL)updateCoreDataWithEntity:(NSDictionary *)entity name:(NSString *)name predicate:(NSString *)predicate; 43 44 /** 45 * 从Entity中删除数据 46 * 47 * @param entity 更新内容 48 * @param name Entity名字 49 * @param predicate 更新条件 50 * 51 * @return 是否删除成功 52 */ 53 - (BOOL)deleteCoreDataWithName:(NSString *)name predicate:(NSString *)predicate; 54 55 @end
CoreDataManager.m
1 // 2 // CoreDataManager.m 3 // UCaiYuan 4 // 5 // Created by wanyakun on 16/1/6. 6 // Copyright © 2016年 com.ucaiyuan. All rights reserved. 7 // 8 9 #import "CoreDataManager.h" 10 11 @interface CoreDataManager () 12 /** 13 * 负责应用和数据直接的交互CRUD 14 */ 15 @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 16 /** 17 * 添加持久化存储库(比如SQLite数据库) 18 */ 19 @property (strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 20 /** 21 * 代表CoreData模型文件 22 */ 23 @property (strong, nonatomic) NSManagedObjectModel *managedObjectModel; 24 @end 25 26 @implementation CoreDataManager 27 28 #pragma mark - 单例 29 +(CoreDataManager *)defaultManager { 30 static dispatch_once_t predicate; 31 static CoreDataManager * defaultManager; 32 dispatch_once(&predicate, ^{ 33 defaultManager=[[CoreDataManager alloc] init]; 34 }); 35 return defaultManager; 36 } 37 38 #pragma mark - public method 39 /** 40 * 插入单条数据到Entity 41 * 42 * @param entity 实体字典 43 * @param name Entity名称 44 */ 45 - (BOOL)insertCoreDataWithEntity:(NSDictionary *)entity name:(NSString *)name 46 { 47 NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self.managedObjectContext]; 48 [object setValuesForKeysWithDictionary:entity]; 49 // 利用上下文对象,将数据同步到持久化存储库 50 NSError *error = nil; 51 BOOL success = [self.managedObjectContext save:&error]; 52 if (!success) { 53 NSLog(@"保存数据到数据库错误:%@",[error localizedDescription]); 54 return NO; 55 } 56 return YES; 57 } 58 59 /** 60 * 根据查询条件从实体中查询数据 61 * 62 * @param entityName Entity名称 63 * @param predicate 查询条件 64 * 65 * @return 字典数组 66 */ 67 - (NSMutableArray *)selectCoreDataFromName:(NSString *)name predicate:(NSString *)predicate 68 { 69 NSFetchRequest *request = [[NSFetchRequest alloc] init]; 70 71 NSEntityDescription *entity = [NSEntityDescription entityForName:name inManagedObjectContext:self.managedObjectContext]; 72 request.entity = entity; 73 74 if (predicate) { 75 NSPredicate *predicateObject = [NSPredicate predicateWithFormat:predicate]; 76 request.predicate = predicateObject; 77 } 78 79 NSError *error; 80 NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&error]; 81 NSMutableArray *resultArray = [NSMutableArray array]; 82 83 for (NSManagedObject *object in fetchedObjects) { 84 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 85 for (NSPropertyDescription *property in entity.properties) { 86 NSString *key = property.name; 87 id value = [object valueForKey:key]; 88 [dict setValue:value forKey:key]; 89 } 90 [resultArray addObject:dict]; 91 } 92 return resultArray; 93 } 94 95 /** 96 * 更新单条数据到Entity 97 * 98 * @param entity 更新内容 99 * @param name Entity名字 100 * @param predicate 更新条件 101 * 102 * @return 是否更新成功 103 */ 104 - (BOOL)updateCoreDataWithEntity:(NSDictionary *)entity name:(NSString *)name predicate:(NSString *)predicate 105 { 106 NSFetchRequest *request = [[NSFetchRequest alloc] init]; 107 108 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:name inManagedObjectContext:self.managedObjectContext]; 109 request.entity = entityDescription; 110 111 if (predicate) { 112 NSPredicate *predicateObject = [NSPredicate predicateWithFormat:predicate]; 113 request.predicate = predicateObject; 114 } 115 116 NSError *error; 117 NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&error]; 118 119 for (NSManagedObject *object in fetchedObjects) { 120 [object setValuesForKeysWithDictionary:entity]; 121 } 122 123 BOOL success = [self.managedObjectContext save:&error]; 124 if (!success) { 125 NSLog(@"更新数据到数据库错误:%@",[error localizedDescription]); 126 return NO; 127 } 128 return YES; 129 } 130 131 /** 132 * 从Entity中删除数据 133 * 134 * @param entity 更新内容 135 * @param name Entity名字 136 * @param predicate 更新条件 137 * 138 * @return 是否删除成功 139 */ 140 - (BOOL)deleteCoreDataWithName:(NSString *)name predicate:(NSString *)predicate 141 { 142 NSFetchRequest *request = [[NSFetchRequest alloc] init]; 143 144 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:name inManagedObjectContext:self.managedObjectContext]; 145 request.entity = entityDescription; 146 147 if (predicate) { 148 NSPredicate *predicateObject = [NSPredicate predicateWithFormat:predicate]; 149 request.predicate = predicateObject; 150 } 151 152 NSError *error; 153 NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&error]; 154 155 for (NSManagedObject *object in fetchedObjects) { 156 [self.managedObjectContext deleteObject:object]; 157 } 158 159 BOOL success = [self.managedObjectContext save:&error]; 160 if (!success) { 161 NSLog(@"删除数据出错:error:%@",error); 162 return NO; 163 } 164 return YES; 165 } 166 167 #pragma mark - private method 168 //获取Documents路径 169 - (NSURL *)applicationDocumentsDirectory 170 { 171 return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 172 } 173 174 - (NSString *)bundleName 175 { 176 return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; 177 } 178 179 #pragma mark - getter setter 180 - (NSManagedObjectContext *)managedObjectContext 181 { 182 if (_managedObjectContext == nil) { 183 _managedObjectContext = [[NSManagedObjectContext alloc] init]; 184 _managedObjectContext.persistentStoreCoordinator = [self persistentStoreCoordinator]; 185 } 186 return _managedObjectContext; 187 } 188 189 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 190 { 191 if (_persistentStoreCoordinator == nil) { 192 _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; 193 194 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[self bundleName]]; 195 NSError *error = nil; 196 if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 197 NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 198 abort(); 199 } 200 } 201 return _persistentStoreCoordinator; 202 } 203 204 - (NSManagedObjectModel *)managedObjectModel 205 { 206 if (_managedObjectModel == nil) { 207 _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 208 } 209 return _managedObjectModel; 210 } 211 212 @end
自己写简单CoreDataManager封装对CoreData操作
标签:
原文地址:http://www.cnblogs.com/wanyakun/p/5124099.html