iOS-CoreData 데이터 영구화

4362 단어

1. NSManagedObjectContext 생성

  - (NSManagedObjectContext *)creatManagedObjectContext
{
    //1、 
    // 
    //modelName  ( xcdatamodeld )
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"modelName" withExtension:@"momd"];
    // 
    NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    //2、     // 
    NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    // 
    NSString *docStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [docStr stringByAppendingPathComponent:@"mySqlite.sqlite"];    NSLog(@"path = %@", sqlPath);
    NSURL *sqlUrl = [NSURL fileURLWithPath:sqlPath];
    // 
    [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqlUrl options:nil error:nil];
    //3、 
    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    // 
    context.persistentStoreCoordinator = store;
    
    return context;
    
}

2. Entity(솔리드)에 대한 작업


조회
  #pragma mark -  
- (void)queryData:(id)data
{

    NSError *error = nil;
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Book"];
    
    NSMutableArray *resultArray = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (resultArray == nil) {
        NSLog(@"error = %@",error);
    }else{
        NSLog(@"query successful!");
    }
    
    for (Book *book in resultArray) {
        NSLog(@"name==%@------price==%@
",book.name,book.price); } }

삽입
   #pragma mark -  
- (void)insertData:(id)data
{
    Book *book = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
    book.name = @" ";
    book.price = [NSNumber numberWithFloat:10.5];
    
    NSError *error = nil;
    
    BOOL isSaveSuccess = [_myAppdelegate.managedObjectContext save:&error];
    if (!isSaveSuccess) {
        NSLog(@"Error:%@",error);
    }else{
        NSLog(@"Save successful!");
    }
    
}

삭제
   #pragma mark -  
- (void)deleteData:(id)date
{
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    
    NSEntityDescription *book = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
    request.entity = book;
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@" "];
    request.predicate = predicate;
    
    NSError *error = nil;
    
    NSMutableArray *fetchResult = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (fetchResult == nil) {
        NSLog(@"delete error = %@",error);
    }else{
        NSLog(@"delete successful!");
    }
    for (Book *book in fetchResult) {
        [_myAppdelegate.managedObjectContext deleteObject:book];
    }
    if (![_myAppdelegate.managedObjectContext save:&error]) {
        NSLog(@"Error == %@,%@",error,error.userInfo);
    }else{
        NSLog(@"delete save successful !");
    }
}

업데이트
    #pragma mark -  
- (void)updateData:(id)data
{
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    
    NSEntityDescription *book = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:_myAppdelegate.managedObjectContext];
    request.entity = book;
    // 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@" "];
    request.predicate = predicate;
    
    NSError *error = nil;
    
    NSMutableArray *fetchResult = [[_myAppdelegate.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (fetchResult == nil) {
        NSLog(@"Error= %@",error);
    }else{
        NSLog(@"update successful!");
    }
    
    for (Book *book in fetchResult) {
        book.name = @" ";
    }
    [_myAppdelegate.managedObjectContext save:&error];
    
}

좋은 웹페이지 즐겨찾기