Resuming an Upload

A file upload can be suspended automatically (e.g., by network failure) or manually (e.g., by user interruption). The suspended upload can later be resumed so as to continue the upload from the point where it was interrupted.

The following sample code shows how to resume a suspended upload.

Swift 3:

  • // Instantiate a bucket.
    let bucket = KiiUser.current()!.bucket(withName: "MyBucket")
    
    // Get the transfer manager of the bucket.
    let manager = bucket.transferManager()
    
    // Prepare an array for storing KiiUploader instances.
    let uploadEntries : [KiiUploader]
    
    do{
      // Get all KiiUploader instances.
      uploadEntries = try manager.getDownloadEntries() as! [KiiUploader]
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    for uploader in uploadEntries {
      // If the upload status is "suspended"
      if uploader.info().status() == .rtStatus_SUSPENDED {
        // Create a progress block.
        let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
          let info = transferObject.info()
          print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
        }
    
        do{
          // Resume uploading.
          try uploader.transfer(progressBlock: progress)
        } catch let error as NSError {
          // Handle the error.
          return
        }
      }
    }
  • // Instantiate a bucket.
    let bucket = KiiUser.current()!.bucket(withName: "MyBucket")
    
    // Get the transfer manager of the bucket.
    let manager = bucket.transferManager()
    
    // Prepare an array for storing KiiUploader instances.
    let uploadEntries : [KiiUploader]
    
    do{
      // Get all KiiUploader instances.
      uploadEntries = try manager.getDownloadEntries() as! [KiiUploader]
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    for uploader in uploadEntries {
      // If the upload status is "suspended"
      if uploader.info().status() == .rtStatus_SUSPENDED {
        // Create a progress block and a completion block.
        let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
          let info = transferObject.info()
          print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
        }
        let completion : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
          if error != nil {
            // Handle the error.
            return
          }
        }
    
        // Resume uploading.
        uploader.transfer(progressBlock: progress, andCompletionBlock: completion)
      }
    }

Objective-C:

  • // Instantiate a bucket.
    KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:@"MyBucket"];
    
    // Get the transfer manager of the bucket.
    KiiRTransferManager *manager = [bucket transferManager];
    
    // Get all KiiUploader instances.
    NSError *error = nil;
    NSArray *uploadEntries = [manager getUploadEntries:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    for (KiiUploader *uploader in uploadEntries) {
      // If the upload status is "suspended"
      if ([[uploader info] status] == KiiRTStatus_SUSPENDED) {
        // Create a progress block.
        KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
          KiiRTransferInfo *info = [transferObject info];
          NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
        };
    
        // Resume uploading.
        [uploader transferWithProgressBlock:progress
                                   andError:&error];
        if (error != nil) {
          // Handle the error.
          NSLog(@"Transfer error happens");
          return;
        }
      }
    }
  • // Instantiate a bucket.
    KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:@"MyBucket"];
    
    // Get the transfer manager of the bucket.
    KiiRTransferManager *manager = [bucket transferManager];
    
    // Get all KiiUploader instances.
    NSError *error = nil;
    NSArray *uploadEntries = [manager getUploadEntries:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    for (KiiUploader *uploader in uploadEntries) {
      // If the upload status is "suspended"
      if ([[uploader info] status] == KiiRTStatus_SUSPENDED) {
        // Create a progress block and a completion block.
        KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
          KiiRTransferInfo *info = [transferObject info];
          NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
        };
        KiiRTransferBlock completion = ^(id <KiiRTransfer> transferObject, NSError *retError) {
          if (retError != nil) {
            // Handle the error.
            NSLog(@"Transfer error happens");
            return;
          }
        };
    
        // Resume uploading.
        [uploader transferWithProgressBlock:progress
                         andCompletionBlock:completion];
      }
    }

Here is what is happening in the sample code:

  • Create a KiiBucket instance of the bucket that has the target KiiObject.
  • Create a KiiRTransferManager instance by calling the transferManager() method.
  • Get a list of KiiUploader instances by calling the getUploadEntries() method.
  • Define a progress and completion blocks.
  • Call the transfer(progressBlock:andCompletionBlock) method to resume.

Checking the status of a KiiUploader

The above sample code gets a KiiRTransferInfo object by executing the info() method of the KiiUploader. From this KiiRTransferInfo object, you can get its upload progress information. This information includes the bytes transferred, the bytes in total, and the status (e.g., ongoing or suspended).