前几天刚写完下载,最近运行发现一个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就解决了,我遇到的问题,基本上百度的话,很难说清楚,所以这次我解决了,我就记录一下,给大家有所帮助,给我也能以后再遇到,能够有个清除的记录

你可能感兴趣的内容
0条评论
PH

Philip-Morton

这家伙太懒了,什么都没留下
Owner