Never getting uploading data progress in google AP

2019-04-07 10:32发布

I am using GoogleDrive API in my application to upload files from my application. So far I succeeded and finding good results for uploading all types of files.

I followed Google example to upload files.

I am trying to show the progress, dependent on file size, while uploading. I have gone through many classes in the google given example code but there isn't much result.

Steps followed to get the file uploading progress :

I have found below method In GTLServices.m

- (void)setUploadProgressBlock:(GTLServiceUploadProgressBlock)block {
  [uploadProgressBlock_ autorelease];
  uploadProgressBlock_ = [block copy];

  if (uploadProgressBlock_) {
    // As above, we need the fetcher to call us back when bytes are sent.
    SEL sentDataSel = @selector(objectFetcher:didSendBytes:totalBytesSent:totalBytesExpectedToSend:);
    [[self objectFetcher] setSentDataSelector:sentDataSel];
  }
}

After that in MyViewController.m written like this

 [self.driveService setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long numberOfBytesRead, unsigned long long dataLength) {
  NSLog(@"uploading");

}];

Above NSLog never getting excused because of this I am unable to get uploaded data bytes. By debugging I come across as uploadProgressBlock_ always showing nil. may be this resone my block handler not getting executed.

Please suggest me if I did any mistake to work it out. If any one have idea to get the bytes of data uploaded to google drive for a file please suggest me. Your suggestions are more useful to my work.

Thanks in advance.

Consolidated Answer by taking our folks suggestions:

Here is the code to get upload progress information

GTLServiceTicket *uploadTicket= [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,GTLDriveFile *updatedFile,NSError *error)
        {
            if (error == nil)
            {
                [self.delegate completedFileUploadingToGoogleDrive];
                NSError *fileDeletionError=nil;
                [[NSFileManager defaultManager] removeItemAtPath:absalutePath error:&fileDeletionError];
            } else
            {
                [self.delegate errorOccoredWhileUploadingToGoogleDrive:error];
            }
        }];

        [uploadTicket setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long totalBytesWritten, unsigned long long totalBytesExpectedToWrite) {
            [self.delegate uploadedFileInfoInKB:(float)totalBytesExpectedToWrite/1024 and:(float)totalBytesWritten/1024];
        }];

Happy Coding!!

3条回答
对你真心纯属浪费
2楼-- · 2019-04-07 10:34

I recently used FabioL's answer but in Swift 2.3, it looks like this

    let serviceTicket = service.executeQuery(query, completionHandler:  { (ticket, insertedFile , error) -> Void in
        let myFile = insertedFile as? GTLDriveFile

        if error == nil {
            alert.title = "Image Uploaded"
            alert.message = "100%"
            if progressView != nil {
                progressView!.progress = 1
            }
        } else {
            print("An Error Occurred! \(error)")
            alert.title = "There was an error uploading"
        }
    })

    //update UI on progress
    serviceTicket.uploadProgressBlock = {(ticket, written, total) in
        if progressView != nil {
            let progressCalc: Float = (Float(written)/Float(total))
            alert.message = "\(progressCalc*100)%"
            progressView!.progress = progressCalc
        }
    }

As the upload happens it updates a ProgressView and on completion it changes the title and message of an UIAlertViewController

查看更多
干净又极端
3楼-- · 2019-04-07 10:43

I have succeeded in using the upload block callback... assign it to the GTLServiceTicket object you obtain from the executeQuery method.

Sample:

GTLServiceTicket *uploadTicket =  [self.driveService executeQuery:query  completionHandler:^(GTLServiceTicket *ticket,  GTLDriveFile *insertedFile, NSError *error) 
                                           { // completion 
                                           }];


        [uploadTicket setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long totalBytesWritten, unsigned long long totalBytesExpectedToWrite) 
         {
             // progress
         }
查看更多
看我几分像从前
4楼-- · 2019-04-07 10:50

I suspect it may have to do with the lack of the NS_BLOCKS_AVAILABLE #define having been set in your project. If so, then instead of using -setUploadProgressBlock:, I think you need to define a method like:

        - (void)ticket:(GTLServiceTicket *)ticket
 hasDeliveredByteCount:(unsigned long long)numberOfBytesRead
      ofTotalByteCount:(unsigned long long)dataLength;

And then specify it as the callback to use in your code, like:

 [self.driveService setUploadProgressSelector:@selector(ticket:hasDeliveredByteCount:ofTotalByteCount:)];

(Search for uploadProgressSelector in GTLService.h; there's a comment there that may be helpful.)

查看更多
登录 后发表回答