iOS.데이터 영구화.PersistenceLayer.Core Data
19984 단어 Core Data
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Persistence04NoteCoreDataDAO : NSObject
//
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
//
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
//
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
@end
#import "Persistence04NoteCoreDataDAO.h"
@implementation Persistence04NoteCoreDataDAO
@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:[self managedObjectModel]];
[_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
#import <CoreData/CoreData.h>
#import "Persistence04NoteCoreDataDAO.h"
#import "Persistence04Note.h"
#import "NoteManagedObject.h"
@interface Persistence04NoteDAO : Persistence04NoteCoreDataDAO
+ (Persistence04NoteDAO*)sharedManager;
// Note
-(int) create:(Persistence04Note*)model;
// Note
-(int) remove:(Persistence04Note*)model;
// Note
-(int) modify:(Persistence04Note*)model;
//
-(NSMutableArray*) findAll;
//
-(Persistence04Note*) findById:(Persistence04Note*)model;
@end
#import "Persistence04NoteDAO.h"
@implementation Persistence04NoteDAO
static Persistence04NoteDAO *sharedManager = nil;
+ (Persistence04NoteDAO*)sharedManager
{
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedManager = [[self alloc] init];
[sharedManager managedObjectContext];
});
return sharedManager;
}
// Note
-(int) create:(Persistence04Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NoteManagedObject *note = [NSEntityDescription insertNewObjectForEntityForName:@"Persistence04Note" 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:(Persistence04Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Persistence04Note" 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:(Persistence04Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Persistence04Note" 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:@"Persistence04Note" 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) {
Persistence04Note *note = [[Persistence04Note alloc] init];
note.date = mo.date;
note.content = mo.content;
[resListData addObject:note];
}
return resListData;
}
//
-(Persistence04Note*) findById:(Persistence04Note*)model
{
NSManagedObjectContext *cxt = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Persistence04Note" 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];
Persistence04Note *note = [[Persistence04Note alloc] init];
note.date = mo.date;
note.content = mo.content;
return note;
}
return nil;
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SwiftUI 에서 CoreData 를 업데이트하면 dismiss 현상 및 대책 마련SwiftUI에 놀라운 현상이 있어 공유합니다. 모FirstView에서 SecondViewNavigationLink로 바뀌면서 목적지를 이전한 View가 코어데이터를 업데이트한 후SecondViewdismiss에 의...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.