1.首先创建父类吧重用的代码写在里边

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h> @interface DBCenter : NSObject @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (NSURL *) modelURL; //need to be overwrite
- (NSURL *) storeURL; //need to be overwrite
- (NSString *) storeDirectoryPath;
- (NSString *) entityName;
- (void) saveContext; @end
#import "DBCenter.h"

@interface DBCenter ()

@end

@implementation DBCenter

- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
} // NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Resources" withExtension:@"bundle"]];
// NSURL *modelURL = [bundle URLForResource:@"LECPlayerDataModel" withExtension:@"momd"];
NSURL *modelURL = [self modelURL]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
} - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
} // Create the coordinator and store _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [self storeURL]; NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code: userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} return _persistentStoreCoordinator;
} - (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
} NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
} #pragma mark -
- (NSURL *) modelURL {
return nil;
} - (NSURL *) storeURL {
return nil;
} - (NSString *) storeDirectoryPath {
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *dbDirectory = [documentDirectory stringByAppendingPathComponent:@"DB"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbDirectory]) {
[fileManager createDirectoryAtPath:dbDirectory
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
return dbDirectory;
} - (NSString *) entityName {
return nil;
} - (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
} @end

2.然后对于某一个存储模块独立出来继承

@interface DBCenterNewsExtend : DBCenter

+ (DBCenterNewsExtend *) sharedNewsDBCenter;

/*启动广告*/
- (LaunchingAdItemModel *) launchingAdItem;
- (void) fullFillLaunchingAdItemModelWithLaunchingAdItem:(LaunchingAdItem *) launchingAdItem;
- (void) saveLaunchingAdItemDataModel;
- (void) removeLaunchingAdItem; /*新闻阅读记录*/
- (BOOL) hasReadWithNewsId:(NSString *) newsId;
- (void) addReadRecordWithNewsId:(NSString *) newsId; /*新闻收藏记录*/
- (BOOL) hasFavoriteWithNewsId:(NSString *) newsId;
- (void) addFavoriteRecordWithNewsDisplayItem:(NewsDisplayItem *) newsDisplayItem;
- (void) removeFavoriteRecordWithNewsId:(NSString *) newsId;
- (NSArray *) favoriteNewsList; //NSArray<NewsParamItem> /*搜索关键字列表*/
- (void) addSearchKeywordRecord:(NSString *) keyword;
- (NSArray *) keywordsList; //NSArray<NewsSearchKeywordModel>
- (void) cleanKeywordsList; @end
@interface DBCenterNewsExtend ()

@property (nonatomic, strong) LaunchingAdItemModel *launchingAdItemModel;
@property (nonatomic, strong) NSMutableArray *mNewsReadRecordsList;
@property (nonatomic, strong) NSMutableArray *mNewsDisplayModelList;
@property (nonatomic, strong) NSMutableArray *mNewsDisplayItemList;
@property (nonatomic, strong) NSMutableArray *mSearchKeywordsList; - (void) loadLaunchingAdItemModel;
- (NSString *) launchingAdEntityName; - (void) loadReadRecordsList;
- (NSString *) newsReadRecordEntityName;
- (NewsReadRecordItemModel *) newsReadRecordItemModelWithNewsId:(NSString *) newsId; - (void) loadFavoriteRecordsList;
- (NSString *) newsDisplayItemEntityName;
- (NewsDisplayItem *) newsDisplayItemFromNewsDisplayDataModel:(NewsDisplayItemDataModel *) newsDisplayItemDataModel;
- (NewsDisplayItemDataModel *) insertNewsDisplayItemDataModelFromNewsDisplayItem:(NewsDisplayItem *) newsDisplayItem;
- (NewsDisplayItem *) newsDisplayItemWithNewsId:(NSString *) newsId;
- (NewsDisplayItemDataModel *) newsDisplayItemDataModelWithNewsId:(NSString *) newsId; - (void) loadSearchKeywordsList;
- (NSString *) searchKeywordEntityName;
- (NewsSearchKeywordModel *) searchDataModelWithKeyword:(NSString *) keyword; @end @implementation DBCenterNewsExtend - (id) init {
if (self = [super init]) {
[self loadLaunchingAdItemModel];
[self loadReadRecordsList];
[self loadFavoriteRecordsList];
[self loadSearchKeywordsList];
}
return self;
} #pragma mark -
#pragma mark Overwrite
- (NSURL *) modelURL {
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"NewsModel" ofType:@"momd"];
NSURL *modelURL = [NSURL URLWithString:modelPath];
return modelURL;
} - (NSURL *) storeURL {
NSString *directoryPath = [self storeDirectoryPath];
NSString *storePath = [directoryPath stringByAppendingPathComponent:@"NewsModel.sqlite"];
return [NSURL fileURLWithPath:storePath];
} #pragma mark -
#pragma mark Public Methods
+ (DBCenterNewsExtend *) sharedNewsDBCenter {
static DBCenterNewsExtend *newsDBCenter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
newsDBCenter = [[DBCenterNewsExtend alloc] init];
});
return newsDBCenter;
} - (LaunchingAdItemModel *) launchingAdItem {
return _launchingAdItemModel;
} - (void) fullFillLaunchingAdItemModelWithLaunchingAdItem:(LaunchingAdItem *) launchingAdItem {
if (!_launchingAdItemModel) {
_launchingAdItemModel = [NSEntityDescription insertNewObjectForEntityForName:[self launchingAdEntityName] inManagedObjectContext:self.managedObjectContext];
[self saveContext];
}
_launchingAdItemModel.adId = launchingAdItem.adId;
_launchingAdItemModel.adTitle = launchingAdItem.adTitle;
_launchingAdItemModel.adImgUrl = launchingAdItem.adImgUrl;
_launchingAdItemModel.adWebUrl = launchingAdItem.adWebUrl;
_launchingAdItemModel.adEndTime = launchingAdItem.adEndTime;
[self saveContext];
} - (void) saveLaunchingAdItemDataModel {
[self saveContext];
} - (void) removeLaunchingAdItem {
[[self managedObjectContext] deleteObject:_launchingAdItemModel];
_launchingAdItemModel = nil;
} - (BOOL) hasReadWithNewsId:(NSString *) newsId {
bool hasRead = NO;
if ([self newsReadRecordItemModelWithNewsId:newsId]) {
hasRead = YES;
}
return hasRead;
} - (void) addReadRecordWithNewsId:(NSString *) newsId {
if (![self newsReadRecordItemModelWithNewsId:newsId]) { NewsReadRecordItemModel *newsReadRecordItemModel = [NSEntityDescription insertNewObjectForEntityForName:[self newsReadRecordEntityName] inManagedObjectContext:self.managedObjectContext];
newsReadRecordItemModel.newsId = newsId;
[_mNewsReadRecordsList addObject:newsReadRecordItemModel];
[self saveContext];
}
} /*新闻收藏记录*/
- (BOOL) hasFavoriteWithNewsId:(NSString *) newsId {
for (NewsDisplayItem *newsDisplayItem in _mNewsDisplayItemList) {
if (newsDisplayItem.newsDisplayParamItemList.count > ) {
NewsDisplayParamItem *displayParamItem = newsDisplayItem.newsDisplayParamItemList[];
if ([displayParamItem.newsId isEqualToString:newsId]) {
return YES;
}
}
}
return NO;
} - (void) addFavoriteRecordWithNewsDisplayItem:(NewsDisplayItem *) newsDisplayItem {
NewsDisplayParamItem *newsDisplayParamItem;
if (newsDisplayItem.newsDisplayParamItemList.count > ) {
newsDisplayParamItem = newsDisplayItem.newsDisplayParamItemList[];
}
NSString *newsId = newsDisplayParamItem.newsId;
if (![self hasFavoriteWithNewsId:newsId]) {
[self insertNewsDisplayItemDataModelFromNewsDisplayItem:newsDisplayItem];
}
} - (void) removeFavoriteRecordWithNewsId:(NSString *) newsId {
NewsDisplayItemDataModel *newsDisplayItemDataModel = [self newsDisplayItemDataModelWithNewsId:newsId];
if (newsDisplayItemDataModel) {
[[self managedObjectContext] deleteObject:newsDisplayItemDataModel];
[self saveContext];
} NewsDisplayItem *newsDisplayItem = [self newsDisplayItemWithNewsId:newsId];
if (newsDisplayItem) {
[_mNewsDisplayItemList removeObject:newsDisplayItem];
}
} - (NSArray *) favoriteNewsList {
return _mNewsDisplayItemList;
} /*搜索关键字列表*/
- (void) addSearchKeywordRecord:(NSString *) keyword {
if (![self searchDataModelWithKeyword:keyword]) {
NewsSearchKeywordModel *newsSearchKeywordModel = [NSEntityDescription insertNewObjectForEntityForName:[self searchKeywordEntityName] inManagedObjectContext:self.managedObjectContext];
newsSearchKeywordModel.keyword = keyword;
[_mSearchKeywordsList addObject:newsSearchKeywordModel];
[self saveContext];
}
} - (NSArray *) keywordsList {
NSMutableArray *mKeywordList = [[NSMutableArray alloc] init];
for (NewsSearchKeywordModel *keywordModel in _mSearchKeywordsList) {
[mKeywordList addObject:keywordModel.keyword];
}
return mKeywordList;
} - (void) cleanKeywordsList {
for (NewsSearchKeywordModel *searchKeywordModel in _mSearchKeywordsList) {
[[self managedObjectContext] deleteObject:searchKeywordModel];
}
[_mSearchKeywordsList removeAllObjects];
[self saveContext];
} #pragma mark -
#pragma mark Public Methods
- (void) loadLaunchingAdItemModel {
NSFetchRequest* request=[[NSFetchRequest alloc] init];
NSEntityDescription* userItemDataModelDescription = [NSEntityDescription entityForName:[self launchingAdEntityName] inManagedObjectContext:self.managedObjectContext];
[request setEntity:userItemDataModelDescription];
NSArray* fetchResultList = [self.managedObjectContext executeFetchRequest:request error:nil];
if (fetchResultList.count > ) {
_launchingAdItemModel = fetchResultList[];
}
} - (NSString *) launchingAdEntityName {
return @"LaunchingAdItemModel";
} - (void) loadReadRecordsList {
NSFetchRequest* request=[[NSFetchRequest alloc] init];
NSEntityDescription* userItemDataModelDescription = [NSEntityDescription entityForName:[self newsReadRecordEntityName] inManagedObjectContext:self.managedObjectContext];
[request setEntity:userItemDataModelDescription];
NSArray* fetchResultList = [self.managedObjectContext executeFetchRequest:request error:nil];
_mNewsReadRecordsList = [NSMutableArray arrayWithArray:fetchResultList];
} - (NSString *) newsReadRecordEntityName {
return @"NewsReadRecordItemModel";
} - (NewsReadRecordItemModel *) newsReadRecordItemModelWithNewsId:(NSString *) newsId {
for (NewsReadRecordItemModel *newsReadRecordItemModel in _mNewsReadRecordsList) {
if ([newsReadRecordItemModel.newsId isEqualToString:newsId]) {
return newsReadRecordItemModel;
}
}
return nil;
} - (void) loadFavoriteRecordsList {
_mNewsDisplayModelList = [[NSMutableArray alloc] init];
_mNewsDisplayItemList = [[NSMutableArray alloc] init]; NSFetchRequest* request=[[NSFetchRequest alloc] init];
NSEntityDescription* userItemDataModelDescription = [NSEntityDescription entityForName:[self newsDisplayItemEntityName] inManagedObjectContext:self.managedObjectContext];
[request setEntity:userItemDataModelDescription];
NSArray* fetchResultList = [self.managedObjectContext executeFetchRequest:request error:nil];
_mNewsDisplayModelList = [NSMutableArray arrayWithArray:fetchResultList]; for (NewsDisplayItemDataModel *newsDisplayItemDataModel in _mNewsDisplayModelList) {
NewsDisplayItem *newsDisplayItem = [self newsDisplayItemFromNewsDisplayDataModel:newsDisplayItemDataModel];
[_mNewsDisplayItemList addObject:newsDisplayItem];
}
} - (NSString *) newsDisplayItemEntityName {
return @"NewsDisplayItemDataModel";
} - (NewsDisplayItem *) newsDisplayItemFromNewsDisplayDataModel:(NewsDisplayItemDataModel *) newsDisplayItemDataModel {
NewsDisplayItem *newsDisplayItem = [[NewsDisplayItem alloc] init];
newsDisplayItem.displayType = [newsDisplayItemDataModel.displayType integerValue];
newsDisplayItem.subscriptType = [newsDisplayItemDataModel.subscriptType integerValue]; NewsDisplayParamItem *newsDisplayParamItem = [[NewsDisplayParamItem alloc] init];
newsDisplayParamItem.newsId = newsDisplayItemDataModel.newsId;
newsDisplayParamItem.newsTitle = newsDisplayItemDataModel.newsTitle;
newsDisplayParamItem.newsBrief = newsDisplayItemDataModel.newsBrief;
newsDisplayParamItem.newsDetailType = [newsDisplayItemDataModel.newsDetailType integerValue];
newsDisplayParamItem.imagesList = [newsDisplayItemDataModel.newsImageList componentsSeparatedByString:@" "]; newsDisplayItem.newsDisplayParamItemList = @[newsDisplayParamItem]; return newsDisplayItem;
} - (NewsDisplayItemDataModel *) insertNewsDisplayItemDataModelFromNewsDisplayItem:(NewsDisplayItem *) newsDisplayItem {
NewsDisplayItemDataModel *newsDisplayItemDataModel = [NSEntityDescription insertNewObjectForEntityForName:[self newsDisplayItemEntityName] inManagedObjectContext:self.managedObjectContext];
newsDisplayItemDataModel.displayType = @(newsDisplayItem.displayType);
newsDisplayItemDataModel.subscriptType = @(newsDisplayItem.subscriptType); if (newsDisplayItem.newsDisplayParamItemList.count > ) {
NewsDisplayParamItem *newsDisplayParamItem = newsDisplayItem.newsDisplayParamItemList[]; newsDisplayItemDataModel.newsId = newsDisplayParamItem.newsId;
newsDisplayItemDataModel.newsBrief = newsDisplayParamItem.newsBrief;
newsDisplayItemDataModel.newsDetailType = @(newsDisplayParamItem.newsDetailType);
newsDisplayItemDataModel.newsTitle = newsDisplayParamItem.newsTitle; NSMutableString *newsImageListString = [[NSMutableString alloc] init];
for (int i = ; i < newsDisplayParamItem.imagesList.count; i++) {
NSString *imageUrl = newsDisplayParamItem.imagesList[i];
[newsImageListString appendString:imageUrl];
if (i < newsDisplayParamItem.imagesList.count - ) {
[newsImageListString appendString:@" "];
}
}
newsDisplayItemDataModel.newsImageList = newsImageListString;
} [_mNewsDisplayModelList addObject:newsDisplayItemDataModel];
[_mNewsDisplayItemList addObject:newsDisplayItem]; [self saveContext]; return newsDisplayItemDataModel;
} - (NewsDisplayItem *) newsDisplayItemWithNewsId:(NSString *) newsId {
for (NewsDisplayItem *newsDisplayItem in _mNewsDisplayItemList) {
if (newsDisplayItem.newsDisplayParamItemList.count > ) {
NewsDisplayParamItem *displayParamItem = newsDisplayItem.newsDisplayParamItemList[];
if ([displayParamItem.newsId isEqualToString:newsId]) {
return newsDisplayItem;
}
}
}
return nil;
} - (NewsDisplayItemDataModel *) newsDisplayItemDataModelWithNewsId:(NSString *) newsId {
for (NewsDisplayItemDataModel *newsDisplayItemDataModel in _mNewsDisplayModelList) {
if ([newsDisplayItemDataModel.newsId isEqualToString:newsId]) {
return newsDisplayItemDataModel;
}
}
return nil;
} - (void) loadSearchKeywordsList {
NSFetchRequest* request=[[NSFetchRequest alloc] init];
NSEntityDescription* keywordModelDescription = [NSEntityDescription entityForName:[self searchKeywordEntityName] inManagedObjectContext:self.managedObjectContext];
[request setEntity:keywordModelDescription];
NSArray* fetchResultList = [self.managedObjectContext executeFetchRequest:request error:nil];
_mSearchKeywordsList = [NSMutableArray arrayWithArray:fetchResultList];
} - (NSString *) searchKeywordEntityName {
return @"NewsSearchKeywordModel";
} - (NewsSearchKeywordModel *) searchDataModelWithKeyword:(NSString *) keyword {
for (NewsSearchKeywordModel *newsSearchKeywordModel in _mSearchKeywordsList) {
if ([newsSearchKeywordModel.keyword isEqualToString:keyword]) {
return newsSearchKeywordModel;
}
}
return nil;
} @end

CoreData数据库搭建的更多相关文章

  1. iOS - CoreData 数据库存储

    1.CoreData 数据库 CoreData 是 iOS SDK 里的一个很强大的框架,允许程序员以面向对象的方式储存和管理数据.使用 CoreData 框架,程序员可以很轻松有效地通过面向对象的接 ...

  2. CoreData数据库

        一  CoreData 了解 1 CoreData 数据持久化框架是 Cocoa API 的一部分,首先在iOSS5 版本的系统中出现:      它允许按照 实体-属性-值 模式组织数据: ...

  3. CoreData数据库迁移的操作

    CoreData数据库迁移操作步骤,操作是基于Xcode7. 1.添加新的数据库.选中当前数据库版本:Editor->Add Model Verson,创建一个新的数据库版本. 2.Comman ...

  4. iOS coredata 数据库升级 时报Can't find model for source store

    在coredata 数据库结构被更改后,没根据要求立即建立新version,而是在原version上进行了小修改,之后才想起来建立新版本.并通过以下代码合并数据库, NSError *error = ...

  5. 【原】iOS学习之SQLite和CoreData数据库的比较

    1. SQLite数据库 sqlite数据库操作的基本流程是, 创建数据库, 再通过定义一些字段来定义表格结构, 可以利用sql语句向表格中插入记录, 删除记录, 修改记录, 表格之间也可以建立联系. ...

  6. 服务器数据库搭建流程(CentOs+mysql)

    前言: 服务器上数据库搭建需要知道Linux系统的版本,以前的Ubuntu14.04直接在终端下输入apt-get install (package)便可方便的下载并安装mysql,但是在centOs ...

  7. CoreData数据库升级

    如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下: 1.选中你的mydata.xc ...

  8. iOS:CoreData数据库的使用四(数据库和UITableViewController以及NSFetchedResultsController一起使用)

    CoreData数据库虽然可以和tableview或者UITableViewController一起使用将数据显示在表格上,但是在准备数据的时候,这种方式需要用一个可变数组来装从数据库一次性取出来的所 ...

  9. iOS:CoreData数据库的使用三(数据库和tableView表格一起使用)

    CoreData数据库是用来持久性存储数据的,那么,我们再从该数据库中取出数据干什么呢?明显的是为了对数据做操作,这个过程中可以将它们直观的显示出来,即通过表格的形式显示出来.CoreData配合ta ...

随机推荐

  1. apache jmeter 使用

    ======================= 插件 ======================= 在JMeter 中很多东西都是基于插件技术的, 所以扩展性非常好, 比如下面这些东西都是插件: 1 ...

  2. 系统设计与分析:Alpha版本2成绩汇总

    作业要求 1.作业内容 作业具体要求以及评分标准 2.评分细则 •给出开头和团队成员列表(10’) •给出发布地址以及安装手册(20’) •给出测试报告(40’) •给出项目情况总结(30’)   * ...

  3. Maven 跳过Junit单元测试

    转载自:https://blog.csdn.net/arkblue/article/details/50974957 -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至 ...

  4. QQ第三方登录-python_web开发_django框架

    准备工作 1. 成为QQ互联的开发者 参考链接: <http://wiki.connect.qq.com/%E6%88%90%E4%B8%BA%E5%BC%80%E5%8F%91%E8%80%8 ...

  5. ssd训练之bug:Invalid JPEG data or crop window, data size 565248

    bug信息 tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid JPEG data or crop window ...

  6. 一、man、系统工作、系统检测命令

    目录 一.man命令 (一)常用按键 (二)结构意义 二.常用系统工作命令 (一) echo (二)date (三)reboot (四)powoff (五)wget (六)ps (七)top (八)p ...

  7. luoguP2824 [HEOI2016/TJOI2016]排序(二分答案做法)

    题意 这题的思路实在巧妙. 首先我们肯定无法对区间进行sort,那么考虑如何使得sort简化. 问:如果给的序列是一个0-1序列,让你区间排序,那么怎么做? 答:建一颗线段树维护sum,求出当前区间中 ...

  8. 小程序setData()使用和注意事项

    注意: 直接修改this.data,而不调用this.setData(),是无法改变当前页面的状态的,会导致数据不一致 仅支持可以JSON化的数据 单次设置的数据不能超过1024KB,尽量避免一次设置 ...

  9. 高性能MySQL count(1)与count(*)的差别

    -------------------------------------------------------------------------------------------------第一篇 ...

  10. JAVA的addAll方法

    List和collections工具类都有这个方法!