I am developing a downloading application. I got stuck at a point, when the user comes back after device screen lock
, then my UI gets stuck. Everything gets nil, whether its UITableview
or NSMutableArray
, everything related to UI becomes nil, but however my downloading still goes on.
This happens only when the device(iPhone/iPad) gets automatic screen lock
or user forces screen lock via lock button
. If the application went to background
via home button then everything works fine. I am not able to figure it out whats possibly be missing. Here's the code that i am using
- (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];
}
But the cell value is nil, hence no update while the data is getting downloaded. And if we scroll the UITableview the cell is refreshed and the values are updated. Once the download is complete the application crashes stating
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Edit
This error occurs at cellForRowAtIndexPath. here's the code
-(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;
}
Please help me as i am running out of ideas.