0

I have 2 View Controllers Home and Home Details. In Home I have a table view in which I am showing thumbnail and duration of a video. When I click on a particular row it's details are shown in Home Details. On returning back I am updating that selected row. So for that in viewWillDisappear Method of Home Details I have written following code :

if ([self.delegate respondsToSelector:@selector(changeSelectedBucketData:)]) {
        [self.delegate changeSelectedBucketData:_videoId];
    } 

Now in the Home Controller I have defined that method as:

-(void)changeSelectedBucketData:(NSString*)videoId {

    NSString *dataStr = [NSString stringWithFormat:@"%@bucket_id=%@",kGetBucketById,videoId];

    [[WebServiceCall sharedInstance] sendGetRequestToWebWithData:dataStr success:^(NSDictionary *json) {

        if([[json valueForKey:@"ResponseCode"] integerValue] == 0) {

        } else {
            dispatch_async(dispatch_get_main_queue(), ^{

                [_arrayOfContent replaceObjectAtIndex:selectedIndex withObject:[json valueForKey:@"GetData"]];

                if (_arrayOfContent.count) {

                    TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0]];

                    [self fillDataForIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] forCell:cell];
                }
            });
        }
    } failure:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    }];
}



-(void)fillDataForIndexPath:(NSIndexPath*)indexPath forCell:(TableViewCellHome*)cell{

    NSDictionary *dict = [_arrayOfContent objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[[_arrayOfContent objectAtIndex:indexPath.row] valueForKey:@"video_URL"]];
            [self downloadDurationAtURL:url cellTag:indexPath];
}

Now I have used the following code to Download Duration of a video :

- (NSUInteger)videoDuration:(NSURL *)videoURL {

    AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:videoURL];

    CMTime durationV = videoAVURLAsset.duration;

    return CMTimeGetSeconds(durationV);
}

- (NSString *)videoDurationTextDurationTotalSeconds:(NSUInteger)dTotalSeconds {

    NSUInteger dHours = floor(dTotalSeconds / 3600);
    NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
    NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);

    if (dHours > 0) {

        return [NSString stringWithFormat:@"%i:%02i:%02i",dHours, dMinutes, dSeconds];

    } else {

        return [NSString stringWithFormat:@"%02i:%02i",dMinutes, dSeconds];
    }
}


-(void)downloadDurationAtURL:(NSURL *)videoURL cellTag:(NSIndexPath*)indexPath {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         //retrive image on global queue

        NSUInteger dTotalSeconds = [self videoDuration:videoURL];

        NSLog(@"dTotalSeconds %i",dTotalSeconds);

        if (dTotalSeconds > 0) {

            NSString *videoDurationText = [self videoDurationTextDurationTotalSeconds:dTotalSeconds];

            dispatch_async(dispatch_get_main_queue(), ^{

                 TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
                [[_arrayOfContent objectAtIndex:indexPath.row] setObject : videoDurationText  forKey:@"duration"];
                cell.labelDuration.text = videoDurationText;
                cell.labelDuration.hidden = false;
            });
        }
        else {

        dispatch_async(dispatch_get_main_queue(), ^{
             TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
                 [[_arrayOfContent objectAtIndex:indexPath.row] setObject : @"" forKey:@"duration"];
                cell.labelDuration.hidden = true;
                cell.labelDuration.text = @"";
            });
        }
    });
}

Now problem is that UI is getting blocked until the duration is changed in the cell. I am not able to select a particular row until duration is displayed on the cell. But it is working fine when I display the Home controller for the first time after calling the API. It only gets blocked when I call it from Home detail.

Swati Gupta
  • 354
  • 1
  • 9
  • The `changeSelectedBucketData` method looks like it is called on the main thread, which means that your get request is holding up the UI thread. – Stonz2 Sep 21 '16 at 13:15
  • I tried this but its not helping. if ([self.delegate respondsToSelector:@selector(changeSelectedBucketData:)]) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self.delegate changeSelectedBucketData:_videoId]; }); } – Swati Gupta Sep 21 '16 at 13:24

1 Answers1

0

You need to load the duration asynchronously, like this:

- (void)videoDuration:(NSURL *)videoURL completion:(void (^)(CMTime))durationCallback {
    AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:videoURL];

    [videoAVURLAsset loadValuesAsynchronouslyForKeys:@[ @"duration"] completionHandler:^{
        NSError *error;
        if([videoAVURLAsset statusOfValueForKey:@"duration" error:&error]) {
            NSLog(@"error getting duration: %@", error);
            durationCallback(kCMTimeZero);  // or something
        } else {
            durationCallback(videoAVURLAsset.duration);
        }
    }];
}
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159