Flush API in Mobile SDKs
3 minute read
This guide walks you through the flush API and the different event flushing policies supported by the RudderStack Android (Kotlin) and iOS (Swift) SDKs.
Overview
The RudderStack Android (Kotlin) and iOS (Swift) SDKs provide a flush API that lets you manually flush any stored events irrespective of the configured flush policies.
You can trigger a flush API call as follows:
analytics.flush()The corresponding Java snippet is shown below:
analytics.flush();analytics.flush()The corresponding Objective-C snippet is shown below:
[analytics flush];If you have added a mobile device mode integration plugin, then aflushAPI call will also trigger the integration’sflushAPI, if it is supported.
Flush policies
RudderStack supports three configurable flush policies. By default, all three policies are enabled.
Count flush policy
The CountFlushPolicy triggers a flush call when a predefined threshold for the event count is reached.
| Flush policies | Data type | Notes |
|---|---|---|
flushAt | Integer | The value of this parameter must be within the range of 1 to 100 - the default value is 30. |
Frequency flush policy
The FrequencyFlushPolicy automatically triggers a flush at the specified intervals.
| Flush policies | Data type | Description |
|---|---|---|
flushIntervalInMillis | Long | Minimum allowed value is1 millisecond - the default value is 10 seconds. |
Startup flush policy
The StartupFlushPolicy automatically triggers a one-time flush API call after the Android (Kotlin) SDK is initialized.
Set a flush policy
You can select and set a flush policy depending on your use case.
Selecting a flush policy from the provided list overrides the default flush policy.
The following examples highlight how you can set the FrequencyFlushPolicy in Android (Kotlin) and iOS (Swift) SDKs:
Android (Kotlin)
- Import the flush policy as shown:
import com.rudderstack.sdk.kotlin.core.internals.policies.FrequencyFlushPolicy- Add
FrequencyFlushPolicyduring the SDK initialization, as shown below. Replace theWRITE_KEYandDATA_PLANE_URLparameters in the below snippet with the Android (Kotlin) source write key and your data plane URL.
analytics = Analytics(
configuration = Configuration(
writeKey = BuildConfig.WRITE_KEY,
application = application,
dataPlaneUrl = BuildConfig.DATA_PLANE_URL,
flushPolicies = listOf(
FrequencyFlushPolicy()
)
)
)The corresponding Java snippet is shown below:
FrequencyFlushPolicy frequencyFlushPolicy = new FrequencyFlushPolicy();
ArrayList<FlushPolicy> listOfFlushPolicies = new ArrayList<>();
listOfFlushPolicies.add(frequencyFlushPolicy);
Configuration configuration = new ConfigurationBuilder(this, "WRITE_KEY", "DATA_PLANE_URL")
.setFlushPolicies(listOfFlushPolicies)
.build();
analytics = new JavaAnalytics(configuration);iOS (Swift)
- Import the flush policy as shown:
import RudderStackAnalytics- Add
FrequencyFlushPolicyduring the SDK initialization. Replace theWRITE_KEYandDATA_PLANE_URLparameters in the below snippet with the iOS (Swift) source write key and your data plane URL.
self.analytics = Analytics(configuration: Configuration(
writeKey: "<WRITE_KEY>",
dataPlaneUrl: "<DATA_PLANE_URL>",
flushPolicies: [
FrequencyFlushPolicy()
]
))- Import the flush policy as shown:
@import RudderStackAnalytics;- Add
FrequencyFlushPolicyduring the SDK initialization. Replace theWRITE_KEYandDATA_PLANE_URLparameters in the below snippet with the iOS (Swift) source write key and your data plane URL.
RSSConfigurationBuilder *builder = [[RSSConfigurationBuilder alloc]
initWithWriteKey:@"<WRITE_KEY>"
dataPlaneUrl:@"<DATA_PLANE_URL>"];
[builder setFlushPolicies:@[
[RSSFrequencyFlushPolicy new]
]];
RSSAnalytics *analytics = [[RSSAnalytics alloc]
initWithConfiguration:[builder build]];