controller
//徒步去旅行
@interface ViewController ()
@property (nonatomic,strong) NSMutableArray *array;
@end
@implementation ViewController
-(void)initData
{
//1.先讀本地數據
self.array = [[TQDataBase sharedDataBase] getAllMovies];
}
-(void)requestData
{
AFHTTPRequestOperationManager *man = [AFHTTPRequestOperationManager manager];
[man GET:@"http://api.douban.com/v2/movie/top250?apikey=02d830457f4a8f6d088890d07ddfae47&start=0&count=20" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//清空之前的數據 (數據庫,數據源(array))
if([[TQDataBase sharedDataBase] isDataExists])
{
[[TQDataBase sharedDataBase] deleteAllDatas];
[self.array removeAllObjects];
}
for (NSDictionary *dict in responseObject[@"subjects"]) {
TQMovie *movie = [[TQMovie alloc] init];
movie.movieImage = dict[@"images"][@"small"];
movie.movieTitle = dict[@"title"];
//把movie緩存到數據庫
[[TQDataBase sharedDataBase] insertMovie:movie];
[self.array addObject:movie];
}
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.//1.先讀本地數據
//2.判斷網絡狀態 —> 加載新數據 NO 提示網絡錯誤的信息
//3.加載新數據 —> 把之前數據庫中的數據清空 新數據保存到數據庫[self initData];
//判斷網絡狀態
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if(status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi)
{
[self requestData];
}
else
{
NSLog(@”請檢查網絡連接”);
}
}];//開啓檢測
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”cell”];
TQMovie *movie = self.array[indexPath.row];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:movie.movieImage] placeholderImage:[UIImage imageNamed:@"photo"]];
cell.textLabel.text = movie.movieTitle;
return cell;
}
dataBase
@implementation TQDataBase
{
FMDatabase *_fmDataBase;
}
(instancetype)init
{
self = [super init];
if (self) {
NSString *docPath = [NSString stringWithFormat:@”%@/Documents/movie.db”,NSHomeDirectory()];NSLog(@"%@",NSHomeDirectory()); //創建數據庫 以及 創建表 _fmDataBase = [FMDatabase databaseWithPath:docPath]; // [_fmDataBase open]; NSString *sqlStr = @"create table if not exists Movie (image text, title text)"; [_fmDataBase executeUpdate:sqlStr]; [_fmDataBase close];
}
return self;
}
+(TQDataBase *)sharedDataBase
{
static TQDataBase *dataBase = nil;
if(dataBase == nil)
{
dataBase = [[TQDataBase alloc] init];
}
return dataBase;
}
-(NSMutableArray *)getAllMovies
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[_fmDataBase open];
FMResultSet *set = [_fmDataBase executeQuery:@"select * from Movie"];
while ([set next]) {
TQMovie *movie = [[TQMovie alloc] init];
movie.movieImage = [set stringForColumn:@"image"];
movie.movieTitle = [set stringForColumn:@"title"];
[array addObject:movie];
}
[_fmDataBase close];
return array;
}
-(BOOL)isDataExists
{
BOOL ret = NO;
[_fmDataBase open];
ret = [[_fmDataBase executeQuery:@"select * from Movie"] next];
[_fmDataBase close];
return ret;
}
-(void)deleteAllDatas
{
[_fmDataBase open];
[_fmDataBase executeUpdate:@"delete from Movie"];
[_fmDataBase close];
}
-(void)insertMovie:(TQMovie *)movie
{
[_fmDataBase open];
NSString *sqlStr = @"insert into Movie (image,title) values (?,?)";
[_fmDataBase executeUpdate:sqlStr,movie.movieImage,movie.movieTitle];
[_fmDataBase close];
}