Executing server code via the Kii Cloud SDK for iOS

Here is an example of executing server code via the iOS SDK.

Swift 3:

  • // Instantiate the endpoint.
    let entry = Kii.serverCodeEntry("main")
    
    // Set parameters.
    let argDict = ["username":"name_of_my_friend","password":"password_for_my_friend"]
    
    // Create a container of the parameters.
    let argument = KiiServerCodeEntryArgument(dictionary:argDict)
    
    let result : KiiServerCodeExecResult
    
    do{
      // Execute the server code.
      result = try entry.executeSynchronous(argument)
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    // Parse the result.
    let returnedDict = result.returnedValue()
    let newUser = returnedDict?["returnedValue"];
    
    // Get the number of the exectued steps.
    let execSteps : Int = result.executedSteps()
    
    // Get the version of Node.js on which the server code was executed.
    let executedVersion = result.environmentVersion()
  • // Instantiate the endpoint.
    let entry = Kii.serverCodeEntry("main")
    
    // Set parameters.
    let argDict = ["username":"name_of_my_friend","password":"password_for_my_friend"]
    
    // Create a container of the parameters.
    let argument = KiiServerCodeEntryArgument(dictionary:argDict)
    
    // Execute the server code.
    entry.execute(argument) { (retEntry : KiiServerCodeEntry, retArg : KiiServerCodeEntryArgument?, result : KiiServerCodeExecResult?, error : Error?) in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Parse the result.
      let returnedDict = result?.returnedValue()
      let newUser = returnedDict?["returnedValue"];
    
      // Get the number of the exectued steps.
      let execSteps : Int = result!.executedSteps()
    
      // Get the version of Node.js on which the server code was executed.
      let executedVersion = result.environmentVersion()
    }

Objective-C:

  • // Instantiate the endpoint.
    KiiServerCodeEntry* entry =[Kii serverCodeEntry:@"main"];
    
    // Set parameters.
    NSDictionary* argDict= [NSDictionary dictionaryWithObjectsAndKeys:
                             @"name_of_my_friend", @"username",
                             @"password_for_my_friend", @"password", nil];
    
    // Create a container of the parameters.
    KiiServerCodeEntryArgument* argument= [KiiServerCodeEntryArgument argumentWithDictionary:argDict];
    
    // Execute the server code.
    NSError* error = nil;
    KiiServerCodeExecResult* result = [entry executeSynchronous:argument
                                                      withError:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Parse the result.
    NSDictionary *returnedDict = [result returnedValue];
    NSString *newUser = [returnedDict objectForKey:@"returnedValue"];
    
    // Get the number of the exectued steps.
    int execSteps = (int)[result executedSteps];
    
    // Get the version of Node.js on which the server code was executed.
    KiiServerCodeEnvironmentVersion executedVersion = [result environmentVersion];
  • // Instantiate the endpoint.
    KiiServerCodeEntry* entry =[Kii serverCodeEntry:@"main"];
    
    // Set parameters.
    NSDictionary* argDict= [NSDictionary dictionaryWithObjectsAndKeys:
                             @"name_of_my_friend", @"username",
                             @"password_for_my_friend", @"password", nil];
    
    // Create a container of the parameters.
    KiiServerCodeEntryArgument* argument= [KiiServerCodeEntryArgument argumentWithDictionary:argDict];
    
    // Execute the server code.
    NSError* error = nil;
    [entry execute:argument
         withBlock:^(KiiServerCodeEntry *entry, KiiServerCodeEntryArgument *argument, KiiServerCodeExecResult *result, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Parse the result.
      NSDictionary *returnedDict = [result returnedValue];
      NSString *newUser = [returnedDict objectForKey:@"returnedValue"];
    
      // Get the number of the exectued steps.
      int execSteps = (int)[result executedSteps];
    
      // Get the version of Node.js on which the server code was executed.
      KiiServerCodeEnvironmentVersion executedVersion = [result environmentVersion];
    }];

Here is the brief explanation of the sample code:

  • Creates a KiiServerCodeEntry instance by executing the serverCodeEntry(_:) method with the target endpoint (function) name. You can specify the version of the JavaScript engine for executing the server code with the environmentVersion argument.
  • Prepares custom parameters (the username and password in this sample code) by setting them in an NSDictionary and by creating a KiiServerCodeEntryArgument with the KiiServerCodeEntryArgument(dictionary:) method. Make sure to align the parameter keys with those in the server code so that the server code can properly get the parameter values (in this sample code the keys are "username" and "password").
  • Executes the server code by calling the execute(_:) method with the custom parameters.
    • If there is a logged-in user, Kii Cloud interprets the server code as being executed by this user.
    • If there is no logged-in user, Kii Cloud interprets the server code as being executed by an anonymous user.
  • Gets the result by calling the returnedValue() method.
  • (Optionally) gets the number of the executed steps with the executedSteps() method.
  • (Optionally) gets the version of Node.js with the environmentVersion() method.

The returnedValue() method will give you an NSDictionary with the result stored in the "returnedValue" key (See Data types of return values for some examples). Use the appropriate method to fetch the actual result.

If your server code returns the value after the timeout, you can get the value with the returnedValue() method likewise. See Timeout for the format of the NSDictionary you can get with the method.