简单实现下载后退出依旧进行下载,并且加入本地数据库

前几天刚写完下载,最近运行发现一个BUG,就是当你点击下载后,返回,再进的话,虽然后台会继续将下载下载完,但相关数据因为页面已经被释放,无法加入到数据库,以至于,在“我的下载”中无法显示,以及不能观看本地视频,今天花了两个小时,将这个BUG解决了,其中的关键在于传值!
1.首先我们在实际下载中,分为3种情况:
第一,点击下载,留在当前页面等待下载完毕
第二,点击下载,返回,回去浏览别的页面,之后回到下载页面时,后台并没有下载完毕
第三,点击下载,返回,回去浏览别的页面,之后回到下载页面时,后台已经下载完毕,下载按钮变色。

第一种,不需要解释,之前我就能实现。
第二种,首先我们在我之前封装的下载圆环的didfinish方法中发送一个通知

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Finish");
    NSString*filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]stringByAppendingPathComponent:self.fileName];
    NSData *data = self.recvivedData;
    [data writeToFile:filePath atomically:NO];//将数据写入Documents目录。
    NSLog(@"%@",filePath);
    self.fileName = filePath;
    [self.delegate progressView:self didFinishedWithData:self.recvivedData suggestedWithFileName:self.fileName];
//下面为关键代码
    [[NSNotificationCenter defaultCenter]postNotificationName:@"fileName" object:nil];
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected];
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected fileName:self.fileName];
}

之后,由于我们在没有下载完就返回了,则我们在那个页面接收通知,并且当下载完毕时,让下载按钮变色,就解决了

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(turnButtonColor) name:@"fileName" object:nil];
- (void)turnButtonColor
{
    self.playView.downloadButton.tintColor = [UIColor redColor];
    self.playView.downloadButton.userInteractionEnabled = NO;
}

第三种情况,我们需要重写我封装的圆环的接口,将接口中添加一个model,因为我做的数据库插入是插入的model,如果通过属性传值的话,是无法传到的,所以干脆多给一个接口,这样实打实的传递到数据

- (void)downloadMovie
{
//最后一个数据是我新加的接口
    self.downloadPercent = [[ProgressView alloc] initWithURL:[NSURL URLWithString:self.dailySelected.playUrl] progressViewWithFrame:(CGRectMake(self.playView.downloadButton.frame.origin.x + 30, self.playView.downloadButton.frame.origin.y + self.navigationController.navigationBar.frame.size.height + 20 + 10, 60, 20)) font:[UIFont fontWithName:@"Georgia-BoldItalic" size:12.0] color:[UIColor whiteColor] timeout:5.0 alive:YES radius:0 delegate:self dailySelected:self.dailySelected];
    [self.view addSubview:self.downloadPercent];
    self.playView.downloadButton.userInteractionEnabled = NO;
}

然后直接在didfinish中完成插入操作,这样不管页面是否消失,都能完成下载,并且加入到数据库中

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Finish");
    NSString*filePath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]stringByAppendingPathComponent:self.fileName];
    NSData *data = self.recvivedData;
    [data writeToFile:filePath atomically:NO];//将数据写入Documents目录。
    NSLog(@"%@",filePath);
    self.fileName = filePath;
    [self.delegate progressView:self didFinishedWithData:self.recvivedData suggestedWithFileName:self.fileName];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"fileName" object:nil];
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected];
    [[OpenEyeDataBase shareSqliteManager] insertDownloadWithModel:self.dailySelected fileName:self.fileName];
}

这样BUG就解决了,我遇到的问题,基本上百度的话,很难说清楚,所以这次我解决了,我就记录一下,给大家有所帮助,给我也能以后再遇到,能够有个清除的记录

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,111评论 25 709
  • 文中讲到那个年代党认为无产者天生低劣,他们地位低下,像牲口一样自由。没有人会去干涉他们的行为,无论是小偷还是各种各...
    Joyce小熊阅读 6,298评论 0 0
  • “林花谢了春红,太匆匆。”看着小院中已式微的紫藤,想着春天就要过去了,心底里无端地泛起一丝感伤。 正当此时一阵风吹...
    清溪书苑阅读 3,878评论 5 18
  • 未完成的待续诗,等你挥墨…… 夏雨欲来 小城女婴呱呱落 八六年六月又四日 一生始蹉跎 好景江南舞 江北水城戏说 日...
    萧娜阅读 1,891评论 16 9
  • 昨天坐错公车,几站后才发觉,慌忙下车,站在路边,忽然感觉周遭的环境那么的陌生。每天的生活都在家、公车、写字楼循环,...
    暮云阁主阅读 1,648评论 0 1

友情链接更多精彩内容