setBaseDirectory method

Future<void> setBaseDirectory([
  1. String? path
])

Set the base directory for model storage. Must be called during SDK initialization. Matches Swift: CppBridge.ModelPaths.setBaseDirectory()

On iOS, getApplicationDocumentsDirectory() resolves to NSDocumentDirectory (e.g. Application/<UUID>/Documents). This persists across normal app relaunches on both simulator and physical device. On a physical device it also persists across App Store / TestFlight reinstalls (bundle ID is the container key).

Simulator caveat (expected, not a bug): xcrun simctl install will reuse the existing data container UUID when the app is already installed, but simctl uninstall (or a corrupted container that forces Xcode to re-provision one) allocates a fresh UUID with an empty Documents/. Previously-downloaded models are then not discoverable because the SDK correctly scans the NEW container, not the old one. This mirrors Swift, React Native, and Kotlin behavior.

Implementation

Future<void> setBaseDirectory([String? path]) async {
  final dir = path ?? (await getApplicationDocumentsDirectory()).path;

  final lib = PlatformLoader.loadCommons();
  final setBase = lib
      .lookupFunction<
        Int32 Function(Pointer<Utf8>),
        int Function(Pointer<Utf8>)
      >('rac_model_paths_set_base_dir');

  final dirPtr = dir.toNativeUtf8();
  try {
    final result = setBase(dirPtr);
    if (result != RacResultCode.success) {
      throw SDKException.invalidConfiguration(
        'rac_model_paths_set_base_dir failed: $result',
      );
    }
    _logger.debug('C++ base directory set to: $dir');
  } finally {
    calloc.free(dirPtr);
  }
}