關於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.mapp
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