- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchDataCache:YES];
[self fetchData];
}
- (void)fetchDataCache:(BOOL)isCache
{
if (isCache) {
NSDictionary *dict = [SystemSupport dictFromCache:CACHE_FILE_HOME];
if (dict) {
[self parseLoadData:dict];
}
}else{
if (_request) {
[_request clearDelegatesAndCancel];
[_request release];
_request = nil;
}
_request = [[ASIHTTPRequest alloc] initWithURL:[SystemSupport URLWithBaseURL:URL_HOME withToken:NO]];
_request.delegate = self;
TTDINFO(@"%@",_request.url.absoluteString);
[_request setCachePolicy:ASIDoNotWriteToCacheCachePolicy];
[_request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[_request setDidFinishSelector:@selector(didLoadData:)];
[_request setDidFailSelector:@selector(didLoadDataErrorHandle:)];
[_request startAsynchronous];
}
}
- (void)fetchData{
[self fetchDataCache:NO];
}
- (void)parseLoadData:(NSDictionary *)dict
{
NSArray *ad = [[dict objectForKey:@"data"] objectForKey:KeyAd];
NSArray *tuijian = [BookPic constructArray:[[dict objectForKey:@"data"] objectForKey:KeyTuijian]];
NSArray *re = [BookPic constructArray:[[dict objectForKey:@"data"] objectForKey:KeyRe]];
NSArray *xin = [BookPic constructArray:[[dict objectForKey:@"data"] objectForKey:KeyXin]];
NSDictionary *dictF = [NSDictionary dictionaryWithObjectsAndKeys:ad, KeyAd, tuijian, KeyTuijian, re, KeyRe, xin, KeyXin, nil];
self.dataDict = dictF;
[_tableView reloadData];
//快捷播放器初始化相关
[self dealQuickMusicInfo];
}
- (void)didLoadData:(ASIHTTPRequest *)request{
[self hideLoadingView];
[self hideEmptyView];
NSDictionary *dict = [SystemSupport dictOfRequest:request];
if ([SystemSupport checkJsonDictSuccess:dict]) {
[SystemSupport cacheDict:dict fileName:CACHE_FILE_HOME];
[self parseLoadData:dict];
}else{
if ([self.dataDict count] > 0) {//如果从缓存中读取数据成功,则不显示加载失败
}else{
if (![[request downloadCache] canUseCachedDataForRequest:request]) {
[self showEmptyReloadView];
}
}
}
}
- (void)didLoadDataErrorHandle:(ASIHTTPRequest *)request{
[self hideLoadingView];
if ([self.dataDict count] == 0) {//如果从缓存中读取数据成功,则不显示加载失败
[self showEmptyReloadView];
}
TTDINFO(@"error:%@",[request error]);
}