在新建iOS工程(XCode10)孝扛,未選擇use core data時列吼,如何添加coredata使用
是否勾選了use core data可通過AppDelegate文件看出,添加use core data AppDelegate.h文件中會存在下面這些屬性(XCode7)
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
添加coreData
1疗琉、新建.xcdatamodeld文件
new file -> 選擇core data -> Data Model
新建好后如下
按照步驟創(chuàng)建Entity(創(chuàng)建好以后記得command+s保存冈欢,這是個好習(xí)慣),其中的Relationships在此沒有用到盈简,有興趣可以戳這relationship 不過這是Mac開發(fā)coredata的操作凑耻,不過coredata的操作是一樣的。
2柠贤、創(chuàng)建NSManagedObject subclass
創(chuàng)建之前請注意這兩個地方香浩。
選中model文件,xcode頂部菜單editor臼勉,再create NSManagedObject subclass
會有四個文件
User+CoreDataClass.h
User+CoreDataClass.m
User+CoreDataProperties.h
User+CoreDataProperties.m
新建好以后這些文件基本可以不要動它了邻吭。
2、接下來可以進(jìn)行CoreData操作了
新建DBManager管理類宴霸,繼承NSObject
DBManager.h中
+ (instancetype)shareManager;
// 創(chuàng)建NSManagedObject
- (id)createMO:(NSString *)entityName;
// 保存
- (void)save:(NSManagedObject *)mo;
// 查詢
- (NSArray *)query:(NSString *)entityName predicate:(NSPredicate *)predicate;
// 修改
- (void)update:(NSManagedObject *)mo;
// 刪除
- (void)remove:(NSManagedObject *)m
DBManager.m中
+ (instancetype)shareManager{
static DBManager *instance = nil;
static dispatch_once_t once = 0;
dispatch_once(&once, ^{
instance = [[[self class]alloc]init];
[instance openDB];
});
return instance;
}
- (void)openDB{
// 加載數(shù)據(jù)模型路徑
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"User" withExtension:@"momd"];
// 創(chuàng)建數(shù)據(jù)模型對象
NSManagedObjectModel *managedModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:managedModel];
// 定義數(shù)據(jù)庫文件的路徑
NSURL *dbURL = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingString:@"/Documents/coreData.sqlite"]];
NSError *error = nil;
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbURL options:nil error:&error];
if(error == nil){
NSLog(@"打開數(shù)據(jù)庫文件成功");
}else{
NSLog(@"打開數(shù)據(jù)庫文件失敗 %@", error);
}
managedContext = [[NSManagedObjectContext alloc] init];
managedContext.persistentStoreCoordinator = store;
}
- (id)createMO:(NSString *)entityName{
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedContext];
return managedObject;
}
- (void)save:(NSManagedObject *)mo{
// 添加到上下文容器
[managedContext insertObject:mo];
NSError *error = nil;
if([managedContext save:&error]){
NSLog(@"數(shù)據(jù)保存成功");
}else{
NSLog(@"數(shù)據(jù)保存失敗 %@", error);
}
}
- (NSArray *)query:(NSString *)entityName predicate:(NSPredicate *)predicate{
if(entityName.length == 0){
return nil;
}
// 關(guān)聯(lián)查詢條件
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:entityName];
request.predicate = predicate;
return [managedContext executeFetchRequest:request error:nil];
}
- (void)update:(NSManagedObject *)mo{
NSError *error = nil;
if([managedContext save:nil]){
NSLog(@"修改成功");
}else{
NSLog(@"修改失敗 %@", error);
}
}
- (void)remove:(NSManagedObject *)mo{
if(mo == nil){
return;
}
[managedContext deleteObject:mo];
NSError *error = nil;
if([managedContext save:&error]){
NSLog(@"刪除成功");
}else{
NSLog(@"刪除失敗 %@", error);
}
}
使用coreData對數(shù)據(jù)庫進(jìn)行操作
以保存為例:
DBManager *managed = [DBManager shareManager];
// 保存
User *user = [managed createMO:@"User"];
user.username = @"zhangsan";
user.password = @"123";
user.age = 22;
[managed save:user];
增刪改查按照方法即可囱晴,在進(jìn)行操作后一定要調(diào)用save方法,每調(diào)用save方法前瓢谢,數(shù)據(jù)庫是沒有真正進(jìn)行操作的畸写。
Demo戳這里coreData Demo