您好,登錄后才能下訂單哦!
// NoteManagedObject.h
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import
#import
@interface NoteManagedObject : NSManagedObject
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * content;
@end
//
// NoteManagedObject.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import "NoteManagedObject.h"
@implementation NoteManagedObject
@dynamic date;
@dynamic content;
@end
//
// Note.h
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import
@interface Note : NSObject
@property (nonatomic,strong) NSDate *date;
@property (nonatomic,strong) NSString *content;
@end
//
// Note.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import "Note.h"
@implementation Note
@end
//
// CoreDataDAO.h
// PersistenceLayer
//
//
#import
#import
@interface CoreDataDAO : NSObject
//被管理的對象上下文
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
//被管理的對象模型
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
//持久化存儲協調者
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
@end
//
// CoreDataDAO.m
// PersistenceLayer
//
//
#import "CoreDataDAO.h"
@implementation CoreDataDAO
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#pragma mark - Core Data 堆棧
//返回 被管理的對象上下文
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// 返回 持久化存儲協調者
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
//數據庫文件
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataNotes.sqlite"];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[selfmanagedObjectModel]];
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:nil];
return _persistentStoreCoordinator;
}
// 返回 被管理的對象模型
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel) {
return _managedObjectModel;
}
//模型文件
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataNotes" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
#pragma mark - 應用程序沙箱
// 返回應用程序Docment目錄的NSURL類型
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
//
// NoteDAO.h
// MyNotes
//
//
#import
#import "CoreDataDAO.h"
#import "Note.h"
#import "NoteManagedObject.h"
@interface NoteDAO : CoreDataDAO
+ (NoteDAO*)sharedManager;
//插入Note方法
-(int) create:(Note*)model;
//刪除Note方法
-(int) remove:(Note*)model;
//修改Note方法
-(int) modify:(Note*)model;
//查詢所有數據方法
-(NSMutableArray*) findAll;
//按照主鍵查詢數據方法
-(Note*) findById:(Note*)model;
@end
//
// NoteDAO.m
// MyNotes
//
#import "NoteDAO.h"
@implementation NoteDAO
static NoteDAO *sharedManager = nil;
+ (NoteDAO*)sharedManager
{
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedManager = [[self alloc] init];
[sharedManager managedObjectContext];
});
return sharedManager;
}
//插入Note方法
-(int) create:(Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NoteManagedObject *note = [NSEntityDescription insertNewObjectForEntityForName:@"Note"inManagedObjectContext:cxt];
[note setValue: model.content forKey:@"content"];
[note setValue: model.date forKey:@"date"];
note.date = model.date;
note.content = model.content;
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"插入數據成功");
} else {
NSLog(@"插入數據失敗");
return -1;
}
return 0;
}
//刪除Note方法
-(int) remove:(Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Note" inManagedObjectContext:cxt];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"date = %@", model.date];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *listData = [cxt executeFetchRequest:request error:&error];
if ([listData count] > 0) {
NoteManagedObject *note = [listData lastObject];
[self.managedObjectContext deleteObject:note];
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"刪除數據成功");
} else {
NSLog(@"刪除數據失敗");
return -1;
}
}
return 0;
}
//修改Note方法
-(int) modify:(Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Note" inManagedObjectContext:cxt];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"date = %@", model.date];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *listData = [cxt executeFetchRequest:request error:&error];
if ([listData count] > 0) {
NoteManagedObject *note = [listData lastObject];
note.content = model.content;
NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]){
NSLog(@"修改數據成功");
} else {
NSLog(@"修改數據失敗");
return -1;
}
}
return 0;
}
//查詢所有數據方法
-(NSMutableArray*) findAll
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Note" inManagedObjectContext:cxt];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];
NSError *error = nil;
NSArray *listData = [cxt executeFetchRequest:request error:&error];
NSMutableArray *resListData = [[NSMutableArray alloc] init];
for (NoteManagedObject *mo in listData) {
Note *note = [[Note alloc] init];
note.date = mo.date;
note.content = mo.content;
[resListData addObject:note];
}
return resListData;
}
//按照主鍵查詢數據方法
-(Note*) findById:(Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Note" inManagedObjectContext:cxt];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"date = %@",model.date];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *listData = [cxt executeFetchRequest:request error:&error];
if ([listData count] > 0) {
NoteManagedObject *mo = [listData lastObject];
Note *note = [[Note alloc] init];
note.date = mo.date;
note.content = mo.content;
return note;
}
return nil;
}
@end
//
// NoteBL.h
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import
#import "Note.h"
#import "NoteDAO.h"
@interface NoteBL : NSObject
//插入Note方法
-(NSMutableArray *)createNote:(Note *)model;
//刪除note方法
-(NSMutableArray *)remove:(Note *)model;
//查詢所有數據方法
-(NSMutableArray *)findAll;
//修改note方法
-(NSMutableArray *)modify:(Note *)model;
@end
//
// NoteBL.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014年 ChengZhifeng. All rights reserved.
//
#import "NoteBL.h"
@implementation NoteBL
//插入Note方法
-(NSMutableArray *)createNote:(Note *)model
{
NoteDAO *dao=[NoteDAO sharedManager];
[dao create:model];
return [dao findAll];
}
//刪除note方法
-(NSMutableArray *)remove:(Note *)model
{
NoteDAO *dao=[NoteDAO sharedManager];
[dao remove:model];
return [dao findAll];
}
//修改note方法
-(NSMutableArray *)modify:(Note *)model
{
NoteDAO *dao=[NoteDAO sharedManager];
[dao modify:model];
return [dao findAll];
}
//查詢所有數據方法
-(NSMutableArray *)findAll
{
NoteDAO *dao=[NoteDAO sharedManager];
return [dao findAll];
}
@end
需要提一下的是,這里的NoteDAO用到了單例模式。NoteBL則不需要單例模式。
sqlite文件則會自動在document文件夾下生成,無需理會。用sqlitemanger看了下,里面的數據是不加密的
附上測試的代碼
//找到數據庫文件的路徑。可以觀察到自動生成的數據庫文件
- (IBAction)test:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"路徑:%@",documentsDirectory);
}
- (IBAction)create:(id)sender {
NoteBL *noteBl=[[NoteBL alloc] init];
Note *note=[[Note alloc] init];
note.date = [[NSDate alloc] init];
note.content = @"hello world";
NSMutableArray *dataList = [noteBl createNote:note];
int length = dataList.count;
for(int i=0;i
Note *tmp = [dataList objectAtIndex:i];
NSLog(@"------%d",i);
NSLog(@"date:%@",tmp.date);
NSLog(@"content:%@",tmp.content);
}
}
- (IBAction)remove:(id)sender {
NoteBL *noteBl=[[NoteBL alloc] init];
NSMutableArray *dataList = [noteBl findAll];
int length = dataList.count;
NSLog(@"刪之前:%d條記錄",length);
if(length<=0) return;
Note *tmp = [dataList objectAtIndex:length-1];
[noteBl remove:tmp];
dataList = [noteBl findAll];
length = dataList.count;
NSLog(@"刪之后:%d條記錄",length);
}
- (IBAction)modify:(id)sender {
NoteBL *noteBl=[[NoteBL alloc] init];
NSMutableArray *dataList = [noteBl findAll];
NSLog(@"修改之前:");
Note *tmp = [dataList objectAtIndex:0];
NSLog(@"date:%@",tmp.date);
NSLog(@"content:%@",tmp.content);
[tmp setContent:@"hhahahhahahhaha"];
dataList = [noteBl modify:tmp];
NSLog(@"修改之后:");
tmp = [dataList objectAtIndex:0];
NSLog(@"date:%@",tmp.date);
NSLog(@"content:%@",tmp.content);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。