Saving and Restoring Initialized Information with Gateway

If you need to save the ThingIFAPI and GatewayAPI instances for a long time, you can use the serialization feature to save it in the storage (i.e., iOS's NSUserDefaults).

Saving the instance to the storage is automatically made by the SDK.

After the process relaunched, you can restore the instance as follows:

var apiGateway: ThingIFAPI?
var gatewayLocal: GatewayAPI?
var apiEndNode1: ThingIFAPI?
var apiEndNode2: ThingIFAPI?
var apiEndNode3: ThingIFAPI?

do {
  // Load API instances.
  try apiGateway = ThingIFAPI.loadWithStoredInstance("gateway")
  try gatewayLocal = GatewayAPI.loadWithStoredInstance()
  try apiEndNode1 = ThingIFAPI.loadWithStoredInstance("endnode1")
  try apiEndNode2 = ThingIFAPI.loadWithStoredInstance("endnode2")
  try apiEndNode3 = ThingIFAPI.loadWithStoredInstance("endnode3")
} catch ThingIFError.API_NOT_STORED {
  // Handle the error.
} catch {
  // Handle the error.
}

If you've created multiple ThingIFAPI and GatewayAPI instances with dedicated tags, you can specify the instance to restore with its tag. Simply pass the tag name in the argument of the loadFromStoredInstance method. Please read the Get ThingIFAPI to learn more about the tag.

You can delete the preserved instance with the removeStoredInstance method. Please refer to the reference for more details.

When saving to the NSUserDefaults, we use the key names ThingIFAPI_INSTANCE or ThingIFAPI_INSTANCE_tag name. All information previously stored with these keys will be overwritten.