Storing Data

It is likely that your app will need to store data that is associated with your users. This data could be anything from user preferences to attributes. Parra provides several simple and flexible ways to store this data.

User Info

Parra has direct support for storing several key pieces of information about a user. This includes the user's name and profile picture. This data is stored on the user object and is automatically synchronized with the server. Both of the functions for updating this information are conveniently accessible from the user module on the parra environment value. User info is intentionally limited to the most common and important user data to keep the API simple and easy to use.

struct MyView: View {
    @Environment(\.parra) private var parra

    var body: some View { } // ...

    func updateUserInfo() async throws {
        try await parra.user.updateUser(
            name: "display name",
            firstName: "first name",
            lastName: "last name"
        )
    }

    func updateProfilePicture() async throws {
        let image: UIImage = // ...
        try await parra.user.updateUserProfilePicture(image)
    }
}

Information about your users' accounts are stored on their linked identities. If you want to store additional information about your users, you can use User Properties or User Metadata as described below.

User Properties

User Properties are a simple key-value store that allows you to store arbitrary data associated with a user. This data is stored on the user object and is automatically synchronized with the server.

Usage

You can access the user properties object from the parraUserProperties environment value.

@Environment(\.parraUserProperties) private var parraUserProperties

Once you have the user properties object, you can use it to get and set values. There are convenience methods for getting and setting values of type String, Bool, and Double. Getters are synchronous and return an Optional value. Setters offer both synchronous and asynchronous versions. The asynchronous versions also throw, giving you the ability to error handle, if desired.

let value = parraUserProperties.string(for: "my_custom_key")
try await parraUserProperties.set("some value", for: "my_custom_key")

Key value pairs can be removed using the deleteValue method, which also offers both synchronous and asynchronous versions.

parraUserProperties.deleteValue(for: "my_old_key")

Limits

User properties must adhere to the following limits:

  • 100 properties per user.
  • Keys must be strings and have a maximum length of 128 characters.
  • Values can be strings, booleans, or numbers.
  • String values have a maximum length of 128 characters.

Conflict Resolution

When you set a user property, you can specify how to handle conflicts. Conflicts should be rare, but can occur if your user has multiple devices which have become out of sync. By default, conflicts are resolved by keeping the new value. You can change this behavior by subscribing to the conflict publisher and resolving the conflict as desired.

.onReceive(parraUserProperties.conflictPublisher) { conflict in
    conflict.resolve(.keepNew)
}

User Metadata

User Metadata is another option for storing key-value information associated with your users. It is intended more for true metadata and less for things like user preferences. User Metadata is stored on the user object and is automatically synchronized with the server, but can only be read from within the SDK. Updates to user metadata happen via the Parra API or the Parra Dashboard.

Was this page helpful?