前提是已经知道了有哪些 key 值

Model 类:

.h

@interface ListModel : NSObject

@property (nonatomic, copy)NSString *time;
@property (nonatomic, copy)NSString *cname;
@property (nonatomic, copy)NSString *summary;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *type; - (void)createArray:(NSDictionary *)result
dataSource:(NSMutableArray *)dataSource;

.m

- (void)createArray:(NSDictionary *)result
dataSource:(NSMutableArray *)dataSource
{
UserModel *userModel = [[UserModel alloc]init];
NSArray *array = result[@"news"];
for (NSDictionary *dict in array) {
ListModel *listModel = [[ListModel alloc]init];
listModel.cname = [NSString stringWithFormat:@"%@",dict[@"cname"]];
listModel.summary = [NSString stringWithFormat:@"%@",dict[@"summary"]];
listModel.title = [NSString stringWithFormat:@"%@",dict[@"title"]];
listModel.type = [NSString stringWithFormat:@"%@",dict[@"type"]];
listModel.time = [NSString stringWithFormat:@"%@",dict[@"lastUpdateTime"]];
[dataSource addObject:listModel];
//NSLog(@"cname:%@",listModel.type); //时间戳转换为时间
NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:[listModel.time integerValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yy-MM-dd"];
NSString *beginStr = [formatter stringFromDate:startDate];
listModel.time = beginStr;
if (!([userModel selectTable].count > )) {
[userModel insert:listModel];
}
}
}

FMDB:

- (BOOL)delteSqlite {
if ([self isSqliteExist]) {
NSString *path = [NSTemporaryDirectory()stringByAppendingString:@"user.db"];
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error;
[manager removeItemAtPath:path error:&error];
if (error) {
NSLog(@"delete sqlite failed");
}else{
NSLog(@"delete sqlite success");
}
return YES;
}else{
NSLog(@"sqlite isn't exist");
return NO;
}
return NO;
} #pragma mark - 检测本地文件是否存在
- (BOOL)isSqliteExist {
NSString *path = [NSTemporaryDirectory()stringByAppendingString:@"user.db"];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:path]) {
NSLog(@"sqlite is exist");
return YES;
}else{
NSLog(@"sqlite isn't exist, prepare to create");
return NO;
}
} #pragma mark -- 建数据库
- (void)openDB {
NSString *path = [NSTemporaryDirectory()stringByAppendingString:@"user.db"];
NSLog(@"path:%@",path);
_db = [FMDatabase databaseWithPath:path];
if ([_db open]) {
//建表
BOOL result = [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS NewsInfo(id integer PRIMARY KEY AUTOINCREMENT,cname text NOT NULL,summary text NOT NULL,title text NOT NULL,type text NOT NULL,time text NOT NULL)"];
if (result) {
NSLog(@"create table success");
}else{
NSLog(@"create tabble success");
[_db close];
}
}else{
[_db close];
NSLog(@"open db failed");
}
} #pragma mark - 查询数据库
- (NSMutableArray *)selectTable
{
if (![_db open]) {
[self openDB];
}
NSMutableArray *tempArray = [NSMutableArray array];
if ([_db open]) {
FMResultSet *resultSet = [_db executeQuery:@"select *from NewsInfo;"];
while ([resultSet next]) {
ListModel *listModel = [[ListModel alloc]init];
listModel.cname = [resultSet objectForColumnName:@"cname"];
listModel.summary = [resultSet objectForColumnName:@"summary"];
listModel.title = [resultSet objectForColumnName:@"title"];
listModel.type = [resultSet objectForColumnName:@"type"];
listModel.time = [resultSet objectForColumnName:@"time"];
[tempArray addObject:listModel];
}
[_db close];
}
return tempArray;
} #pragma mark - 插入进表
- (void)insert:(ListModel *)model
{
if (![_db open]) {
[self openDB];
}
[_db executeUpdate:@"INSERT INTO NewsInfo(cname,summary,title,type,time)VALUES(?,?,?,?,?)",model.cname,model.summary,model.title,model.type,model.time];
} #pragma mark - 修改某个值
- (void)update:(NSString *)value to:(NSString *)key {
if (![_db open]) {
[self openDB];
}
if ([_db open]) {
NSString *updateSql = [NSString stringWithFormat:@"update NewsInfo set %@='%@'",key,value];
BOOL res = [_db executeUpdate:updateSql]; if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[_db close];
}
}

VC:

- (void)viewDidLoad {
[super viewDidLoad];
[self addBtn]; self.title = @"新闻";
_userModel = [[UserModel alloc]init];
_dataSource = [[NSMutableArray alloc]initWithCapacity:];
[self.view addSubview:self.tableView]; [self isRequestData];
} #pragma mark - 添加一个删除数据库的按钮
- (void)addBtn {
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"delete" style:UIBarButtonItemStylePlain target:self action:@selector(deleteSqlite)];
self.navigationItem.rightBarButtonItem = item;
} #pragma mark 删除数据库按钮方法
- (void)deleteSqlite {
[_userModel delteSqlite];
} #pragma mark - 本地数据库有值就不请求数据,取本地数据库值
- (void)isRequestData {
if ([_userModel isSqliteExist]) {
_dataSource = [_userModel selectTable];
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
}else{
//创建一个异步队列解析 json,防止阻塞主线程
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, );
dispatch_async(queue, ^{
[self urlStr];
});
}
} #pragma mark -- 解析 JSON
- (void)urlStr
{
NSURL *url = [NSURL URLWithString:URLSTR];
NSURLSession *session = [NSURLSession sharedSession];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSError *error1;
//解析 json,返回字典,这里解析出来是 unicode 编码,不影响正常显示
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error1]; ListModel *listModel = [[ListModel alloc]init];
[listModel createArray:dict dataSource:_dataSource]; //数据源开始是空的,因为网络等原因...等数据源有值了,在主线程刷新 TabelView
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
}];
[task resume];
} #pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cell_id = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
}
ListModel *listModel = _dataSource[indexPath.row];
cell.textLabel.text = listModel.title;
cell.detailTextLabel.text = listModel.time;
return cell;
} #pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ListModel *listModel = _dataSource[indexPath.row];
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
detailVC.titleStr = listModel.cname;
detailVC.contentStr = listModel.summary;
} #pragma mark -- getter
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}

完整代码,见 github

iOS 学习 - 22 异步解析 JSON,使用 FMDB 存储,TableView 显示的更多相关文章

  1. 前端学习之——js解析json数组

    ** 前端学习之——js解析json数组** 解析json数组即对JSONArray的遍历 一.对于标准的json数组如: var result=[{"flag":1," ...

  2. iOS学习之数据解析

    解析:按照约定好的格式提取数据的过程叫做解析; 后台开发人员按照约定好的格式存入数据,前端开发人员按照约定的格式读取数据; 主流的格式: XML / JSON 前端和后台都能识别的格式;  XML解析 ...

  3. 《项目经验》--后台一般处理程序向前台JS文件传递JSON,JS解析JSON,将数据显示在界面--显示在DropDownList 或 显示在动态创建的table中

    http://blog.csdn.net/mazhaojuan/article/details/8599167 先看一下我要实现的功能界面: 这篇文章主要介绍:后台一般处理程序把从数据库查找的数据,转 ...

  4. iOS网络-02-数据解析(JSON与XML)

    数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需要对 ...

  5. 【原】iOS学习之XML与JSON两种数据结构比较和各自底层实现

    1.XML与JSON两种数据结构的优缺点 1> XML 优点:
 格式统一, 符合标准
 容易与其他系统进行远程交互, 数据共享比较方便 
 缺点: XML文件格式文件庞大, 格式复杂, 传输占 ...

  6. ANDROID_MARS学习笔记_S02_013_Gson解析json串

    1.MainActivity.java package com.json; import java.io.IOException; import java.io.StringReader; impor ...

  7. Python学习--22 异步I/O

    在同步IO中,线程启动一个IO操作然后就立即进入等待状态,直到IO操作完成后才醒来继续执行.而异步IO方式中,线程发送一个IO请求到内核,然后继续处理其他的事情,内核完成IO请求后,将会通知线程IO操 ...

  8. 【原】iOS学习之UIStoryboardSegue解析

    在 Storyboard 的可视化编程中,跳转界面就是按住 Ctrl 使用鼠标头一条连线就可以解决,相当的简单!本篇博客主要就是介绍这条连线,在iOS中,这条连线也是一个对象,也有其自己的初始化方法和 ...

  9. iOS学习22之视图控制器

    1.自定义视图 1> 概述   定义视图:系统标准UI之外,自己组合而出的新的视图. 定义视图的优点: iOS提供了很多UI组件,借助它们我们可以实现不同的功能.尽管如此,实际开发中,我们还需要 ...

随机推荐

  1. hdu4831 Scenic Popularity(线段树)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4831 题目大概意思就是有多个风景区和休息区,每个风景区有热度,休息区的热度与最接近的分景区的热度相同, ...

  2. php使用js对表格进行排序

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-type" content=&quo ...

  3. JavaScriptSerializer 序列化json 时间格式

    利用JavaScriptSerializer 序列化json 时间格式,得到的DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,显然要进行转换 1.利用字符串直 ...

  4. MS SQL验证字符串是否包含有大小写字母

    昨晚有实现一个小功能,就是在MS SQL Server中,检查字符串是否包含有大小写字母.通常应用在字符串的复杂度. ) = N'SDFfgGRYJhhTYUJ' IF LOWER(@s) COLLA ...

  5. DevExtreme 学习应用[1]

    DevExtreme学习开发 [1] 用HTML开发手机应用,看一哈帮助文档觉得还很不错. 在开发前一定要安装DevExteme  下载连接地址: ftp://211.101.1.108/DevExp ...

  6. DOM官方定义

    DOM Document Object Model 文档对象模型 DOM的官方定义:W3C的DOM,可以使程序或者脚本(JS或AS\JScript),动态的访问或者操作文档的内容.结构.样式. DOM ...

  7. Java Web项目RSA加密

    最近做的一个项目,服务器为Java,采用SSH框架,客户端为Android和IOS.当用户登录时,从客户端向服务器提交用户名和密码.这就存在一个问题,如果数据包在网络上被其他人截取了,密码就有可能泄露 ...

  8. 【JAVA并发编程实战】9、锁分段

    package cn.study.concurrency.ch11; /** * 锁分段 * @author xiaof * */ public class StripedMap { //同步策略:就 ...

  9. Xdebug文档(三)堆栈跟踪

    当xdebug激活时,PHP一旦要显示通知.警告或错误时,xdebug 显示堆栈跟踪信息.这个堆栈信息能跟据你的需要来配置显示. Xdebug显示的堆栈跟踪都是以保守数量状态显示信息.因为大量的信息处 ...

  10. OData V4 系列 .net应用

    OData 学习目录 添加 OData Client Code Generator 扩展 添加OData T4生成工具 修改 T4 模板的 MetadataDocumentUri 运行Web项目,之后 ...