// NoteCoreDataLearn
目前創(chuàng)新互聯(lián)已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、天全網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
NoteManagedObject :
@property nonatomic retain
@property nonatomic retain
@end
//
// NoteManagedObject.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
"NoteManagedObject.h"
NoteManagedObject
@dynamic
content;
@end
//
// Note.h
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
@interface
@property nonatomicstrong
@property nonatomicstrong
@end
//
// Note.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
#import
@implementation
@end
//
// CoreDataDAO.h
// PersistenceLayer
//
//
CoreDataDAO :
被管理的對象上下文
@property readonly strong nonatomic
被管理的對象模型
@property readonly strong nonatomic
持久化存儲協(xié)調(diào)者
(, , ) *persistentStoreCoordinator;
- ( *)applicationDocumentsDirectory;
@end
//
// CoreDataDAO.m
// PersistenceLayer
//
//
"CoreDataDAO.h"
@implementation
managedObjectContext = ;
managedObjectModel = ;
persistentStoreCoordinator = ;
#pragma mark - Core Data
返回 被管理的對象上下文
- ( *)managedObjectContext
{
_managedObjectContext
_managedObjectContext
}
NSPersistentStoreCoordinator
(coordinator) {
NSManagedObjectContext
setPersistentStoreCoordinator
}
_managedObjectContext
}
返回 持久化存儲協(xié)調(diào)者
- ( *)persistentStoreCoordinator
{
_persistentStoreCoordinator
_persistentStoreCoordinator
}
//數(shù)據(jù)庫文件
applicationDocumentsDirectory
alloc initWithManagedObjectModel
_persistentStoreCoordinator
:
:storeURL
:
:];
_persistentStoreCoordinator
}
返回 被管理的對象模型
- ( *)managedObjectModel
{
_managedObjectModel
_managedObjectModel
}
//模型文件
mainBundle URLForResource withExtension
alloc initWithContentsOfURL
_managedObjectModel
}
#pragma mark -
// DocmentNSURL
- ( *)applicationDocumentsDirectory
{
defaultManager URLsForDirectoryNSDocumentDirectory inDomainsNSUserDomainMask lastObject
}
@end
//
// NoteDAO.h
// MyNotes
//
//
"CoreDataDAO.h"
#import
"NoteManagedObject.h"
NoteDAO :
+ (*)sharedManager;
//Note
-() create:(*)model;
//Note
-() remove:(*)model;
//Note
-() modify:(*)model;
查詢所有數(shù)據(jù)方法
NSMutableArray
按照主鍵查詢數(shù)據(jù)方法
-(*) findById:(*)model;
@end
//
// NoteDAO.m
// MyNotes
//
"NoteDAO.h"
@implementation
*sharedManager = ;
+ (*)sharedManager
{
dispatch_once_t
(&once, ^{
= [[ ] ];
managedObjectContext
});
sharedManager
}
//Note
-() create:(*)model
{
NSManagedObjectContext
insertNewObjectForEntityForNameinManagedObjectContext
[note : model. :];
[note : model. :];
note. = model.;
note. = model.;
*savingError = ;
([. :&savingError]){
();
} {
();
-;
}
;
}
//Note
-() remove:(*)model
{
NSManagedObjectContext
NSEntityDescription NSEntityDescription
: :cxt];
NSFetchRequest NSFetchRequest
[request :entityDescription];
NSPredicate NSPredicate
, model.];
[request :predicate];
*error = ;
*listData = [cxt :request :&error];
([listData ] > ) {
*note = [listData ];
managedObjectContext
*savingError = ;
([. :&savingError]){
();
} {
();
-;
}
}
;
}
//Note
-() modify:(*)model
{
NSManagedObjectContext
NSEntityDescription NSEntityDescription
: :cxt];
NSFetchRequest NSFetchRequest
[request :entityDescription];
NSPredicate NSPredicate
, model.];
[request :predicate];
*error = ;
*listData = [cxt :request :&error];
([listData ] > ) {
*note = [listData ];
note. = model.;
*savingError = ;
([. :&savingError]){
();
} {
();
-;
}
}
;
}
查詢所有數(shù)據(jù)方法
NSMutableArray
{
NSManagedObjectContext
NSEntityDescription NSEntityDescription
: :cxt];
NSFetchRequest NSFetchRequest
[request :entityDescription];
*sortDescriptor = [[ ] : :];
[request :sortDescriptor];
*error = ;
*listData = [cxt :request :&error];
NSMutableArray NSMutableArray
( *mo listData) {
*note = [[ ] ];
note. = mo.;
note. = mo.;
[resListData :note];
}
resListData;
}
按照主鍵查詢數(shù)據(jù)方法
-(*) findById:(*)model
{
NSManagedObjectContext
NSEntityDescription NSEntityDescription
: :cxt];
NSFetchRequest NSFetchRequest
[request :entityDescription];
NSPredicate NSPredicate
,model.];
[request :predicate];
*error = ;
*listData = [cxt :request :&error];
([listData ] > ) {
*mo = [listData ];
*note = [[ ] ];
note. = mo.;
note. = mo.;
note;
}
return nil
}
@end
//
// NoteBL.h
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
#import
"NoteDAO.h"
NoteBL :
//Note
-( *)createNote:( *)model;
//note
-( *)remove:( *)model;
查詢所有數(shù)據(jù)方法
NSMutableArray
//note
-( *)modify:( *)model;
@end
//
// NoteBL.m
// NoteCoreDataLearn
//
// Created by ChengDavid on 14-7-6.
// Copyright (c) 2014 ChengZhifeng. All rights reserved.
//
"NoteBL.h"
@implementation
//Note
-( *)createNote:( *)model
{
*dao=[ ];
[dao :model];
[dao ];
}
//note
-( *)remove:( *)model
{
*dao=[ ];
[dao :model];
[dao ];
}
//note
-( *)modify:( *)model
{
*dao=[ ];
[dao :model];
[dao ];
}
查詢所有數(shù)據(jù)方法
NSMutableArray
{
*dao=[ ];
[dao ];
}
@end
需要提一下的是,這里的NoteDAO用到了單例模式。NoteBL則不需要單例模式。
sqlite文件則會自動在document文件夾下生成,無需理會。用sqlitemanger看了下,里面的數(shù)據(jù)是不加密的
附上測試的代碼
//找到數(shù)據(jù)庫文件的路徑??梢杂^察到自動生成的數(shù)據(jù)庫文件
- ()test:()sender {
NSSearchPathForDirectoriesInDomainsNSDocumentDirectory NSUserDomainMask
*documentsDirectory = [paths :];
(,documentsDirectory);
}
- ()create:()sender {
*noteBl=[[ ] ];
*note=[[ ] ];
note. = [[ ] ];
@"hello world"
*dataList = [noteBl :note];
length = dataList.;
( i=;i
*tmp = [dataList :i];
(,i);
(,tmp.);
(,tmp.);
}
}
- ()remove:()sender {
*noteBl=[[ ] ];
*dataList = [noteBl ];
length = dataList.;
(,length);
(length<=return;
*tmp = [dataList :length-];
[noteBl :tmp];
dataList = [noteBl ];
length = dataList.;
(,length);
}
- ()modify:()sender {
*noteBl=[[ ] ];
*dataList = [noteBl ];
@""
*tmp = [dataList :];
(,tmp.);
@"content:%@"
@"hhahahhahahhaha"
dataList = [noteBl :tmp];
@""
tmp = [dataList :];
(,tmp.);
@"content:%@"
}
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。