CoreData

来源:互联网 发布:英镑符号 mac系统 编辑:程序博客网 时间:2024/06/09 19:47
  Core Data数据持久化是对SQLite的一个升级,它是ios集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类。

(1)NSManagedObjectModel(被管理的对象模型)

       相当于实体,不过它包含 了实体间的关系(2)NSManagedObjectContext(被管理的对象上下文)     操作实际内容    作用:插入数据  查询  更新  删除

(3)NSPersistentStoreCoordinator(持久化存储助理)

      相当于数据库的连接器(4)NSFetchRequest(获取数据的请求)    相当于查询语句 (5)NSPredicate(相当于查询条件)(6)NSEntityDescription(实体结构)(7)后缀名为.xcdatamodel的包    里面的.xcdatamodel文件,用数据模型编辑器编辑   编译后为.momd或.mom文件,这就是为什么文件中没有这个东西,而我们的程序中用到这个东西而不会报错的原因

首先我们要建立模型对象
这里写图片描述
生成模型对象的实体 创建好所需要的Attribute 和Type
然后 Create NSManagerObject…..
这里写图片描述

下面这个截图是创建后自动生成的实体
这里写图片描述
这里写图片描述
解释一下dynamic
dynamic和synthesize有什么区别呢?dynamic的setter和getter方法不能自已定义

然后新建一个类进行增删改查
.h文件

#import <UIKit/UIKit.h>#import "MusicCollect+CoreDataProperties.h"@class AppDelegate;@interface CoreDataModelCollectManager : NSObject//单例+ (CoreDataModelCollectManager *)shareCoreDataModelManager;//保存- (void)saveMusicModelWithSongName:(NSString *)songName                        singerName:(NSString *)singerName                               url:(NSString *)url;//删除- (void)deleteMusicWithSongName:(NSString *)songName;//查找- (NSArray *)fetchSongWithSongName:(NSString *)songName;//更改- (void)updateMusicWithSongName:(NSString *)songName                    newSongName:(NSString *)newSongName                  newSingerName:(NSString *)newSingerName                         newUrl:(NSString *)newUrl;@end

.m文件

#import "CoreDataModelCollectManager.h"#import "AppDelegate.h"@interface CoreDataModelCollectManager ()@property (nonatomic, strong) AppDelegate *appDelegate;@endstatic CoreDataModelCollectManager *manager;@implementation CoreDataModelCollectManager//单例+ (CoreDataModelCollectManager *)shareCoreDataModelManager{    @synchronized(self) {        if (!manager){            manager = [[CoreDataModelCollectManager alloc] init];            manager.appDelegate = [UIApplication sharedApplication].delegate;        }    }    return manager;}//保存- (void)saveMusicModelWithSongName:(NSString *)songName                        singerName:(NSString *)singerName                               url:(NSString *)url{    NSMutableArray *array = [NSMutableArray arrayWithArray:[self fetchSongWithSongName:songName]];    if (array.count > 0){        NSLog(@"已有重复数据");        return;    }    NSEntityDescription *description = [NSEntityDescription entityForName:@"MusicCollect" inManagedObjectContext:self.appDelegate.managedObjectContext];    MusicCollect *music = [[MusicCollect alloc] initWithEntity:description insertIntoManagedObjectContext:self.appDelegate.managedObjectContext];    music.name = songName;    music.singerName = singerName;    music.url = url;    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];    NSLog(@"%@", documentsPath);    [self.appDelegate saveContext];}//删除- (void)deleteMusicWithSongName:(NSString *)songName{    NSMutableArray *array = [NSMutableArray arrayWithArray:[self fetchSongWithSongName:songName]];    if (array.count > 0){        for (MusicCollect *music in array) {            [self.appDelegate.managedObjectContext deleteObject:music];        }    }}//查找- (NSArray *)fetchSongWithSongName:(NSString *)songName{    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MusicCollect" inManagedObjectContext:self.appDelegate.managedObjectContext];    [fetchRequest setEntity:entity];    // Specify criteria for filtering which objects to fetch    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = '%@'", songName];    [fetchRequest setPredicate:predicate];    // Specify how the fetched objects should be sorted    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"                                                                   ascending:YES];    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];    NSError *error = nil;    NSArray *fetchedObjects = [self.appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];    if (fetchedObjects == nil) {        NSLog(@"没有搜到对应歌曲");    }    return fetchedObjects;}//更改- (void)updateMusicWithSongName:(NSString *)songName                    newSongName:(NSString *)newSongName                  newSingerName:(NSString *)newSingerName                         newUrl:(NSString *)newUrl{}@end
0 0
原创粉丝点击