我正在开发一个下载应用程序。我被卡住了,当用户在device screen lock
之后回来时,我的用户界面被卡住了。一切都变为零,无论是UITableview
还是NSMutableArray
,与UI相关的一切都变为零,但是我的下载仍在继续。
仅当设备(iPhone / iPad)获得automatic screen lock
或用户强制screen lock via lock button
时才会发生这种情况。如果应用程序通过主页按钮转到background
,那么一切正常。我无法弄清楚可能会遗漏什么。这是我正在使用的代码
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if (isComingFromBackGround == YES)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[AppDelegate storyboardIdentifier] bundle:Nil];
ViewController *obj = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[obj calledWhenComingFromBackground];
}
}
-(void)calledWhenComingFromBackground
{
NSArray *downloadingArray = [[LibraryAPI sharedInstance] getDownloadingVideos];
if ([downloadingArray count] != 0)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[AppDelegate storyboardIdentifier] bundle:Nil];
DownloadViewController *obj = [storyboard instantiateViewControllerWithIdentifier:@"DownloadViewController"];
[obj setIscomingFromBackground:YES];
[obj notificationsHandler:Nil];
}
}
-(void)notificationsHandler:(NSNotification *)notification
{
for (DownloadingVideo *objDV in currentDownloadingArray)
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:tempStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[httpClient.operationQueue setMaxConcurrentOperationCount:10];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:targetPath2 shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"\t -:Success Block CAlled:- ");
processSuccessBlock(operation, responseObject);
[self badgedValue];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.isCancelled)
{
NSLog(@"the operation is cancelled...");
}
else
{
if(operation.response.statusCode!= 200)
{
NSLog(@"the Faliour Occurred!!!!!!");
processFailureBlock(operation, error);
}
}
[self badgedValue];
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation,NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
NSString *tempStr = operation.request.URL.absoluteString;
for (DownloadingVideo *objDownloading in [[[LibraryAPI sharedInstance] getDownloadingVideos] mutableCopy])
{
NSDictionary *tempDic = [objDownloading dv_tableRepresentation];
if ([[tempDic objectForKey:@"Url"] isEqualToString:tempStr])
{
float tempPercentDone = [[tempDic objectForKey:@"ProgressStatus"] floatValue];
float percentDone = ((float)totalBytesReadForFile) / totalBytesExpectedToReadForFile;
NSString *str = [NSString stringWithFormat:@"%.2f",percentDone];
percentDone = [str floatValue];
float progressOffset1 = (float)totalBytesReadForFile/1024;
float progressOffset2 = (float)totalBytesExpectedToReadForFile/1024;
NSString *strProgressStatus;
NSString *strProgressLblValue;
NSString *strSizeLblValue;
NSString *strSize;
if (tempPercentDone < percentDone)
{
strProgressStatus = [NSString stringWithFormat:@"%0.2f", percentDone];
strProgressLblValue = [NSString stringWithFormat:@"%.0f%% Completed", percentDone*100];
}
else
{
strProgressStatus = [NSString stringWithFormat:@"%0.2f", tempPercentDone];
strProgressLblValue = [NSString stringWithFormat:@"%.0f%% Completed", tempPercentDone*100];
}
strSizeLblValue = [NSString stringWithFormat:@"%.2f/%.2f MB",progressOffset1/1024, progressOffset2/1024];
strSize = [NSString stringWithFormat:@"%.2f MB",progressOffset2/1024];
NSString *strTitle = [tempDic objectForKey:@"Title"];
NSString *strUrl = [tempDic objectForKey:@"Url"];
NSString *strVideoID = [tempDic objectForKey:@"VideoID"];
NSString *strThumbnail = [tempDic objectForKey:@"Thumbnail"];
NSString *strSmallerThumbnail = [tempDic objectForKey:@"SmallerThumbnail"];
NSString *strDuration = [tempDic objectForKey:@"Duration"];
NSString *strYoutubeValue = [tempDic objectForKey:@"YoutubeValue"];
NSString *strM3u8 = [tempDic objectForKey:@"m3u8"];
NSString *strIsDownloadingPaused= @"NO";
DownloadingVideo *newObjDownloading = [[DownloadingVideo alloc] initWithTitle:strTitle url:strUrl videoID:strVideoID thumbnail:strThumbnail smallerThumbnail:strSmallerThumbnail size:strSize duration:strDuration youtubeValue:strYoutubeValue m3u8:strM3u8 progressStatus:strProgressStatus progressLblvalue:strProgressLblValue sizeLblValue:strSizeLblValue isDownloadingPaused:strIsDownloadingPaused];
int newIndex = [[[LibraryAPI sharedInstance] getDownloadingVideos] indexOfObject:objDownloading];
[[LibraryAPI sharedInstance] updateVideoInDownloading:newObjDownloading atIndex:newIndex updateCoredata:NO];
DownloadingVideo *tempNewObjDV = [[[LibraryAPI sharedInstance] getDownloadingVideos] objectAtIndex:newIndex];
NSDictionary *newDic = [tempNewObjDV dv_tableRepresentation];
// Update the corresponding table view cell
NSInteger row = newIndex;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = (UITableViewCell*)[self.tblview cellForRowAtIndexPath:indexPath];
[self setupCell:cell withAttributes:newDic];
break;
}
}
}];
}
[self badgedValue];
[self.tblview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}
但是单元格值为nil,因此在下载数据时不会更新。如果我们滚动UITableview,则刷新单元格并更新值。下载完成后,应用程序崩溃说明
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
修改
此错误发生在cellForRowAtIndexPath。这是代码
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier;
UITableViewCell * cell;
if (indexPath.section == 0)
{
MyIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
DownloadingVideo *objDV = [[[LibraryAPI sharedInstance] getDownloadingVideos] objectAtIndex:indexPath.row];
NSDictionary *dic = [objDV dv_tableRepresentation];
[self setupCell:cell withAttributes:dic];
}
return cell;
}
请帮助我,因为我没有想法。