亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

CoreData使用

發布時間:2020-07-22 15:50:02 來源:網絡 閱讀:338 作者:卓行天下 欄目:開發技術

//  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 (readonlystrongnonatomicNSPersistentStoreCoordinator *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 allocinit];

        [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 allocinitWithKey:@"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 allocinit];

        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 allocinit];

        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 allocinit];

    Note *note=[[Note allocinit];

    note.date = [[NSDate allocinit];

    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 allocinit];

    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 allocinit];

    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);

    

}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

航空| 潞西市| 合水县| 公安县| 佛坪县| 景谷| 托里县| 榆中县| 宁武县| 尖扎县| 安顺市| 舞阳县| 行唐县| 辰溪县| 铜山县| 惠州市| 长阳| 高密市| 新平| 上犹县| 陕西省| 定兴县| 景德镇市| 大英县| 合阳县| 墨脱县| 西宁市| 晋州市| 唐海县| 江陵县| 拜泉县| 左贡县| 呈贡县| 隆尧县| 庆安县| 新巴尔虎右旗| 临朐县| 荣成市| 固阳县| 英超| 郴州市|