Initialization in Swift

Every Ice-based application needs to initialize the Ice run time, and this initialization returns an Ice.Communicator object.

Communicator is a local Swift object that represents an instance of the Ice run time. Most Ice-based applications create and use a single Communicator object, although it is possible and occasionally desirable to have multiple Communicator objects in the same application.

You initialize the Ice run time by calling Ice.initialize, for example:

Swift
let communicator = try Ice.initialize(args: CommandLine.arguments) // returns an Ice.Communicator

initialize scans the argument array for any command-line options that are relevant to the Ice run time. If anything goes wrong during initialization, initialize throws an exception.

This initialize does not modify the argument array. You can use another overload of initialize that removes all Ice-related options from the argument array.

Once you no longer need a Communicator, you must call destroy on this Communicator. The destroy method is responsible for finalizing the instance of the Ice run time embodied by this Communicator. In particular, in an Ice server, destroy waits for operation implementations that are still executing to complete. In addition, destroy ensures that any outstanding threads are joined with and reclaims a number of operating system resources, such as file descriptors and memory. Never allow your application to terminate without calling destroy first.

The general shape of a simple command-line Swift application becomes:

Swift
let communicator = try Ice.initialize(args: CommandLine.arguments)
defer {
    communicator.destroy()
}

See Also