> For the complete documentation index, see [llms.txt](https://docs.indicio.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.indicio.tech/developer/mobile-solutions/mobile-sdk/ios-async.md).

# iOS Async

We have written custom completion handlers for our asynchronous functions in iOS. All exposed suspending functions include an overload for a completion handler that expects a `ProvenError?` and the function result (if not a void function). If the `ProvenError` is not null then there was an error while executing the function and the expected return value will be null.

### Void function example

`agent.start()` is a suspending void function. We can call it like so:

{% tabs %}
{% tab title="Swift" %}

```swift
// Await
let error: ProvenError? = await agent.start(startUpTimeout: nil)
if (error != nil) {
    // Handle error
} else {
    print("Agent Started!")
}

// Or use a completion handler
agent.start(startUpTimeout: nil) { error in
    if (error != nil) {
        // Handle error
    } else {
        print("Agent Started!")
    }
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[agent startStartUpTimeout: nil
    completion:^(ProvenmobileProvenError *_Nullable error){
        if(error){
            // Handle error
        }else{
            NSLog(@"Agent Started!");
        }
    }
];
```

{% endtab %}
{% endtabs %}

### Returning function example

`didExchange.getAll()` is a suspend function that returns an Array of `DidExchangeRecord`. We can call it like so:

{% tabs %}
{% tab title="Swift" %}

```swift
// Await
let (records, error) = await agent.didExchange.getAll()
if (error != nil) {
    print("Error getting connections")
} else {
    print("Connections: \(records!.count)")
}

// Or use a completion handler
agent.didExchange.getAll { records, error in
    if (error != nil) {
        print("Error getting connections")
    } else {
        print("Connections: \(records!.count)")
    }
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[agent.didExchange
    getAllCompletion:^(
        NSArray<ProvenmobileDidExchangeRecord *> *_Nullable records,
        ProvenmobileProvenError *_Nullable error
    ){
        if(error){
            // Handle error
        }else{
            NSLog(@"Connections: %lu", records.count);
        }
    }
];
```

{% endtab %}
{% endtabs %}

## KMPNativeCoroutines

We also have support for [KMPNativeCoroutines](https://github.com/rickclephas/KMP-NativeCoroutines) (1.0.0-ALPHA-23) with Swift. To use it you will need to add it to your project and import the desired functions. Here are some examples:

```swift
import KMPNativeCoroutinesAsync

// Agent.start() example
_ = try await asyncFunction(for: agent.start(startUpTimeout: nil))

// DidExchange.getAll() example
let records = try await asyncFunction(for: agent.didExchange.getAll())
print("Connections: \(records.count)")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.indicio.tech/developer/mobile-solutions/mobile-sdk/ios-async.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
