IOS之分析网易新闻存储数据(CoreData的使用,增删改查)

用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的。

首先:

1、网易新闻用CoreData存储了新闻列表,因为我打开网易新闻的Documents时看到了三个文件:

newsapp.sqlite,newsapp.sqlite-shm,newsapp.sqlite-wal:这三个文件是你在用CoreData时自动生成的。所以我确定他是用coredata存储的数据而不是sqlite数据库。(CoreData优点:能够合理管理内存,避免使用sql的麻烦,高效)

2、网易会隔一断时间请求一次网络,具体时间有可能是隔8个小时或者5个小时或者3个小时都有可能,这个我无法确定时间。反正确实在一定时间后会清空一下数据库并且添加新的请求来的新闻。

3、查看网易新闻后会有一个记录状态,表示已看过,这个也在数据库中存储着。

我这里就简单的实现一下网易新闻的界面,主要讲一下如何用CoreData存储数据,并实现增删改查。

实现的效果:

Demo下载地址:

如果Demo打不开请选择一下版本:

首先关于UItableViewCell的使用,大家可以参考此博文:IOS高访新浪微博界面(讲解如何自定义UITableViewCell,处理@#链接 特殊字符)

接下来是如何创建CoreData:

命名为NewsModel:

添加CoreData框架

导入#import<CoreData/CoreData.h>

贴代码之前需要了解6个对象:

1、NSManagedObjectContext

管理对象,上下文,持久性存储模型对象

2、NSManagedObjectModel

被管理的数据模型,数据结构

3、NSPersistentStoreCoordinator

连接数据库的

4、NSManagedObject

被管理的数据记录

5、NSFetchRequest

数据请求

6、NSEntityDescription

表格实体结构

此外还需要知道.xcdatamodel文件编译后为.momd或者.mom文件

以下是封装好的CoreData管理类CoreDataManager.h:

#import <Foundation/Foundation.h>#import "News.h"#define TableName @"News"@interface CoreDateManager : NSObject@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;- (void)saveContext;- (NSURL *)applicationDocumentsDirectory;//插入数据- (void)insertCoreData:(NSMutableArray*)dataArray;//查询- (NSMutableArray*)selectData:(int)pageSize andOffset:(int)currentPage;//删除- (void)deleteData;//更新- (void)updateData:(NSString*)newsId withIsLook:(NSString*)islook;@end以下是.m的实现://// CoreDateManager.m// SQLiteTest//// Created by rhljiayou on 14-1-8.// Copyright (c) 2014年 rhljiayou. All rights reserved.//#import "CoreDateManager.h"@implementation CoreDateManager@synthesize managedObjectContext = _managedObjectContext;@synthesize managedObjectModel = _managedObjectModel;@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;- (void)saveContext{NSError *error = nil;NSManagedObjectContext *managedObjectContext = self.managedObjectContext;if (managedObjectContext != 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();}}}#pragma mark – Core Data stack// Returns the managed object context for the application.// If the context doesn’t already exist, it is created and bound to the persistent store coordinator for the application.- (NSManagedObjectContext *)managedObjectContext{if (_managedObjectContext != nil) {return _managedObjectContext;}NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];if (coordinator != nil) {_managedObjectContext = [[NSManagedObjectContext alloc] init];[_managedObjectContext setPersistentStoreCoordinator:coordinator];}return _managedObjectContext;}// Returns the managed object model for the application.// If the model doesn’t already exist, it is created from the application’s model.- (NSManagedObjectModel *)managedObjectModel{if (_managedObjectModel != nil) {return _managedObjectModel;}NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NewsModel" withExtension:@"momd"];_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];return _managedObjectModel;}// Returns the persistent store coordinator for the application.// If the coordinator doesn’t already exist, it is created and the application’s store added to it.- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{if (_persistentStoreCoordinator != nil) {return _persistentStoreCoordinator;}NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"NewsModel.sqlite"];NSError *error = nil;_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();}return _persistentStoreCoordinator;}#pragma mark – Application’s Documents directory// Returns the URL to the application’s Documents directory.获取Documents路径- (NSURL *)applicationDocumentsDirectory{return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];}//插入数据- (void)insertCoreData:(NSMutableArray*)dataArray{NSManagedObjectContext *context = [self managedObjectContext];for (News *info in dataArray) {News *newsInfo = [NSEntityDescription insertNewObjectForEntityForName:TableName inManagedObjectContext:context];newsInfo.newsid = info.newsid;newsInfo.title = info.title;newsInfo.imgurl = info.imgurl;newsInfo.descr = info.descr;newsInfo.islook = info.islook;NSError *error;if(![context save:&error]){NSLog(@"不能保存:%@",[error localizedDescription]);}}}//查询- (NSMutableArray*)selectData:(int)pageSize andOffset:(int)currentPage{NSManagedObjectContext *context = [self managedObjectContext];// 限定查询结果的数量//setFetchLimit// 查询的偏移量//setFetchOffsetNSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];[fetchRequest setFetchLimit:pageSize];[fetchRequest setFetchOffset:currentPage];NSEntityDescription *entity = [NSEntityDescription entityForName:TableName inManagedObjectContext:context];[fetchRequest setEntity:entity];NSError *error;NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];NSMutableArray *resultArray = [NSMutableArray array];for (News *info in fetchedObjects) {NSLog(@"id:%@", info.newsid);NSLog(@"title:%@", info.title);[resultArray addObject:info];}return resultArray;}//删除-(void)deleteData{NSManagedObjectContext *context = [self managedObjectContext];NSEntityDescription *entity = [NSEntityDescription entityForName:TableName inManagedObjectContext:context];NSFetchRequest *request = [[NSFetchRequest alloc] init];[request setIncludesPropertyValues:NO];[request setEntity:entity];NSError *error = nil;NSArray *datas = [context executeFetchRequest:request error:&error];if (!error && datas && [datas count]){for (NSManagedObject *obj in datas){[context deleteObject:obj];}if (![context save:&error]){NSLog(@"error:%@",error);}}}//更新- (void)updateData:(NSString*)newsId withIsLook:(NSString*)islook{NSManagedObjectContext *context = [self managedObjectContext];NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"newsid like[cd] %@",newsId];//首先你需要建立一个requestNSFetchRequest * request = [[NSFetchRequest alloc] init];[request setEntity:[NSEntityDescription entityForName:TableName inManagedObjectContext:context]];[request setPredicate:predicate];//这里相当于sqlite中的查询条件,具体格式参考苹果文档//https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.htmlNSError *error = nil;NSArray *result = [context executeFetchRequest:request error:&error];//这里获取到的是一个数组,你需要取出你要更新的那个objfor (News *info in result) {info.islook = islook;}//保存if ([context save:&error]) {//更新成功NSLog(@"更新成功");}}@end此句:NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"NewsModel.sqlite"];生成以后,你可以在Documents下面看到三个文件:人生最大的错误是不断担心会犯错

IOS之分析网易新闻存储数据(CoreData的使用,增删改查)

相关文章:

你感兴趣的文章:

标签云: