addTemporaryStrongReference method

bool addTemporaryStrongReference({
  1. required Object instance,
  2. String? instanceId,
  3. required void onFinalize(
    1. String instanceId
    ),
})

Add a new instance with instanceId as key and instance as the value.

instance is temporarily stored as a strong reference. instance is changed to a weak reference once getInstance is called once with instanceId.

onFinalize is called after instance is garbage collected after being switched to aa weak reference.

This is mainly used when the native platform wants to create a new instance pair, but doesn't want to claim ownership of it.

Returns true if the pair is successfully added. Returns false if the instanceId or instance is already contained in the manager or the instance is a num, bool, or String.

Implementation

bool addTemporaryStrongReference({
  required Object instance,
  String? instanceId,
  required void Function(String instanceId) onFinalize,
}) {
  final String newId = instanceId ?? generateUniqueInstanceId(instance);
  if (addWeakReference(
    instance: instance,
    instanceId: newId,
    onFinalize: onFinalize,
  )) {
    _temporaryStrongReferences[newId] = instance;
    return true;
  }
  return false;
}