十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
// NoteCoreDataLearn
目前创新互联已为超过千家的企业提供了网站建设、域名、网页空间、网站运营、企业网站设计、天全网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。//
// 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
持久化存储协调者
(, , ) *persistentStoreCoordinator;
- ( *)applicationDocumentsDirectory;
@end
//
// CoreDataDAO.m
// PersistenceLayer
//
//
"CoreDataDAO.h"
@implementation
managedObjectContext = ;
managedObjectModel = ;
persistentStoreCoordinator = ;
#pragma mark - Core Data
返回 被管理的对象上下文
- ( *)managedObjectContext
{
_managedObjectContext
_managedObjectContext
}
NSPersistentStoreCoordinator
(coordinator) {
NSManagedObjectContext
setPersistentStoreCoordinator
}
_managedObjectContext
}
返回 持久化存储协调者
- ( *)persistentStoreCoordinator
{
_persistentStoreCoordinator
_persistentStoreCoordinator
}
//数据库文件
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;
查询所有数据方法
NSMutableArray
按照主键查询数据方法
-(*) 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]){
();
} {
();
-;
}
}
;
}
查询所有数据方法
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;
}
按照主键查询数据方法
-(*) 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;
查询所有数据方法
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 ];
}
查询所有数据方法
NSMutableArray
{
*dao=[ ];
[dao ];
}
@end
需要提一下的是,这里的NoteDAO用到了单例模式。NoteBL则不需要单例模式。
sqlite文件则会自动在document文件夹下生成,无需理会。用sqlitemanger看了下,里面的数据是不加密的
附上测试的代码
//找到数据库文件的路径。可以观察到自动生成的数据库文件
- ()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:%@"
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。