最近项目要求只更新发生改变的部分,特别是图片这块,占用内存比较大,每次更新都需要更新全部的内容,浪费资源和时间,于是跟后台商量做了个增量更新,只更新发生改变的部分,中间使用时间戳来保持上次更新的时间,跟后台那边做对比,后台返回需要更新的数据.
-(void)updateTest
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES]; // 防止锁屏
[self notifyMessage:@"开始下载..."];
[self updateOtherPic];
NSUserDefaults * defaults=[NSUserDefaults standardUserDefaults];
lastUpdateTime=[defaults stringForKey:@"lastUpdateTime2"];
// 如果lastUpdateTime==nil 为第一次更新 服务器那边返回全部的图片
NSString * updateURL=@"http://192.168.8.59/update/GetPlusImgUpdateData?";
ASIFormDataRequest * request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:updateURL]];
[request setPostValue:AppSession.ipadUdid forKey:@"uuid"];
[request setPostValue:lastUpdateTime forKey:@"lastUpdateTime"];
[request setRequestMethod:@"POST"];
request.delegate=self;
[request startAsynchronous];
[request setCompletionBlock:^
{
NSDictionary * dict1=[request.responseString objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSArray * array1=[dict1 objectForKey:@"ResponseData"];
if (array1==nil)
{
self.exceptionMessage = @"已是最新数据";
[self notifyMessage:@"已是最新数据"];
}
else
{
self.exceptionMessage = @"获取数据中...";
[self notifyMessage:@"获取数据中..."];
// NSLog(@"array1--->%@",array1);
for (int i =0; i<array1.count; i++)
{
NSString * plupluimgString1=[[array1 objectAtIndex:i]objectForKey:@"ppPluPluId"];
NSString *downloadUrl = [[NSUserDefaults standardUserDefaults] stringForKey:@"download_url"];
if (downloadUrl == nil)
{
[[NSUserDefaults standardUserDefaults] setObject:DefaultUrlPrefix forKey:@"download_url"];
[[NSUserDefaults standardUserDefaults] synchronize];
downloadUrl = [[NSUserDefaults standardUserDefaults] stringForKey:@"download_url"];
}
AppSession.downloadUrl = downloadUrl;
NSString * plupluimgString2=[NSString stringWithFormat:@"%@/%@/%@.jpg",AppSession.downloadUrl,DownloadUrlPluPluImgs,plupluimgString1];
// self.picnameArray 图片的前缀名数组 保存图片时用
[self.picnameArray addObject:plupluimgString1];
[self.updateArray addObject:plupluimgString2];
}
// 拼接好的URL地址放入数组中进行下载
[self updatePluPluimage:self.updateArray];
}
}];
}
-(void)updatePluPluimage:(NSMutableArray*)array
{
if (array.count==0||array==nil)
{
return;
}
else
{
self.exceptionMessage = @"开始更新菜品图片";
[self notifyMessage:@"开始更新菜品图片"];
ASINetworkQueue *picQueue = [[ASINetworkQueue alloc] init];
[picQueue reset];
//下载完成之后 我这边是通过下载路径把下载好的图片根据项目要求做了缩略图的处理.不再详细写缩略图过程
[picQueue setQueueDidFinishSelector:@selector(picQueuedownLoadFinish)];
[picQueue setDelegate:self];
[picQueue setMaxConcurrentOperationCount:1]; //一个个下载
NSString *folderPath = [[NSString alloc] init]; // 文件路径
// 下载的文件夹路径
folderPath = [[DocumentPath stringByAppendingPathComponent: AppImgFolder ] stringByAppendingPathComponent:@"PluPluImgs"];
[FileDefaultManager removeItemAtPath:folderPath error:nil];
[FileManager folderCreate:folderPath];
for (int i=0; i<[array count]; i++)
{
NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];
NSString * picname=[array objectAtIndex:i];
NSURL *url=[NSURL URLWithString:picname];
ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.jpg",[self.picnameArray objectAtIndex:i]]]];
[request setUserInfo:[NSDictionary dictionaryWithObject:picname forKey:@"folderPath"]];
// 进入后台也可以下载
[request setShouldContinueWhenAppEntersBackground:YES];
[request setTimeOutSeconds:30];
// 如果下载失败 重新第二遍
// [request setNumberOfTimesToRetryOnTimeout:2];
[request setDelegate:self];
[picQueue addOperation:request];
// 如果下载失败 放到数组中重新下载
[request setFailedBlock:^{
[self.badfileArray addObject:picname];
}];
[pool1 release];
}
[picQueue go];
}
// 更新完成之后,保存此次更新的时间,下次用
NSDate * locationdate=[NSDate date];
lastUpdateTime= [NSString stringWithFormat:@"%ld", (long)[locationdate timeIntervalSince1970]];
NSUserDefaults * defaults=[NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"lastUpdateTime2"];
[defaults setObject:lastUpdateTime forKey:@"lastUpdateTime2"];
self.exceptionMessage = @"数据更新完成!请重启本应用!";
[self notifyMessage:@"数据更新完成!请重启本应用!"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"update_switch"];//关闭自动更新设置
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
// 如果有下载失败的 把失败的URL地址放到一个数组中 重新调用下载的方法,循环下载.
- (IBAction)badfiledownload:(UIButton *)sender
{
if (self.badfileArray.count==0||self.badfileArray==nil)
{
self.exceptionMessage =@"数据已全部更新";
[self notifyMessage:@"数据已全部更新"];
}
else
{
[self updatePluPluimage:self.badfileArray];
}
}