Downloading an Object Body

The following sample code shows how to download an object body with the resumable transfer.

Swift 3:

  • // Instantiate the target KiiObject.
    let object = KiiObject(uri: "Set the URI of an existing KiiObject here")!
    
    // Specify the file destination.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let downloadFilePath = targetDirectory.appendingPathComponent("sample.mp4")
    
    // Create a downloader.
    let downloader = object.downloader(downloadFilePath)
    
    // Create a progress block.
    let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
      let info = transferObject.info()
      print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
    }
    
    do{
      // Start downloading.
      try downloader.transfer(progressBlock: progress)
    } catch let error as NSError {
      print("transfer error")
      return
    }
  • // Instantiate the target KiiObject.
    let object = KiiObject(uri: "Set the URI of an existing KiiObject here")!
    
    // Specify the file destination.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let downloadFilePath = targetDirectory.appendingPathComponent("sample.mp4")
    
    // Create a downloader.
    let downloader = object!.downloader(downloadFilePath)
    
    // Create a progress block.
    let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
      let info = transferObject.info()
      print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
    }
    
    // Start downloading.
    downloader.transfer(progressBlock: progress, andCompletionBlock: { (transferObject : KiiRTransfer, error ) in
      if error != nil {
        // Handle the error.
        return
      }
    })

Objective-C:

  • NSError *error = nil;
    
    // Instantiate the target KiiObject.
    KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"];
    
    // Specify the file destination.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *downloadFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"];
    
    // Create a downloader.
    KiiDownloader *downloader = [object downloader:downloadFilePath];
    
    // Create a progress block.
    KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
      KiiRTransferInfo *info = [transferObject info];
      NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
    };
    
    // Start downloading.
    [downloader transferWithProgressBlock:progress
                                 andError:&error];
    if (error != nil) {
      // Handle the error.
      NSLog(@"Transfer error!");
      return;
    }
  • // Instantiate the target KiiObject.
    KiiObject *object = [KiiObject objectWithURI:@"Set the URI of an existing KiiObject here"];
    
    // Specify the file destination.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *downloadFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"];
    
    // Create a downloader.
    KiiDownloader *downloader = [object downloader:downloadFilePath];
    
    // Create a progress block.
    KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
      KiiRTransferInfo *info = [transferObject info];
      NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
    };
    
    // Start downloading.
    [downloader transferWithProgressBlock:progress
                     andCompletionBlock:^(id<KiiRTransfer> transferObject, NSError *error) {
      if (error != nil) {
        // Handle the error.
        NSLog(@"Transfer error!");
        return;
      }
    }];

Here is what is happening in the sample code:

  • Create a KiiObject instance.
  • Create a KiiDownloader instance by calling the downloader(_:) method with the file reference.
  • Define a progress block.
  • Start downloading by calling the transfer(progressBlock:andCompletionBlock:_:) method.

Note that the progress block will not be called if you execute the transfer(progressBlock:_:) method (blocking API) from the main thread.

The progress block will be called as the download progresses. The block can reach 100% on the first call if the data size is small.

If the download is interrupted for some reasons, it will be suspended, and an error will be thrown. In this case, you can resume the download (For the steps to resume, see Resuming a Download).