Resuming a Download

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

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

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 KiiDownloader instances.
    let downloadEntries : [KiiDownloader]
    
    do{
      // Get all KiiDownloader instances.
      downloadEntries = try manager.getDownloadEntries() as! [KiiDownloader]
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    for downloader in downloadEntries {
      // If the download status is "suspended"
      if downloader.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 downloading.
          try downloader.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 KiiDownloader instances.
    let downloadEntries : [KiiDownloader]
    
    do{
      // Get all KiiDownloader instances.
      downloadEntries = try manager.getDownloadEntries() as! [KiiDownloader]
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    for downloader in downloadEntries {
      // If the download status is "suspended"
      if downloader.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 downloading.
        downloader.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 KiiDownloader instances.
    NSError *error = nil;
    NSArray *downloadEntries = [manager getDownloadEntries:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    for (KiiDownloader *downloader in downloadEntries) {
      // If the download status is "suspended"
      if ([[downloader 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 downloading.
        [downloader transferWithProgressBlock:progress
                                     andError:&error];
        if (error != nil) {
          // Handle the error.
          NSLog(@"Transfer error!");
          return;
        }
      }
    }
  • // Instantiate a bucket.
    KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:@"MyBucket"];
    
    // Get the transfer manager of the bucket.
    KiiRTransferManager *manager = [bucket transferManager];
    
    // Get all KiiDownloader instances.
    NSError *error = nil;
    NSArray *downloadEntries = [manager getDownloadEntries:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    for (KiiDownloader *downloader in downloadEntries) {
      // If the download status is "suspended"
      if ([[downloader 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!");
            return;
          }
        };
    
        // Resume downloading.
        [downloader 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 KiiDownloader instances by calling the getDownloadEntries() method.
  • Define a progress and completion blocks.
  • Select the KiiDownloader instance to resume, and then call the transfer(progressBlock:andCompletionBlock:) method.

Checking the status of a KiiDownloader

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