How to define Array of primitive values in Realm/React Native

Hi,
I’m creating an app in Realm with Expo/React Native.
I have a huge problem with specifying Schema for one field. I want to have an array of numbers.

My Session Schema looks like:

export class Session extends Realm.Object<Session> {
    _id: Realm.BSON.ObjectId = new Realm.BSON.ObjectId();
    name!: string;
    cube!: string;
    //@ts-ignore
    solves: Realm.List<Solve> = [];
    // @ts-ignore
    validTimes!: number[];
    owner_id!: Realm.BSON.ObjectId;
    used: Date = new Date();
    amount: number = 0;
    fullAmount: number = 0;
    stdev: number = 0;
    average: number = 0;
    best: number = 0;

    static primaryKey = '_id';
}

Problem is with:

validTimes!: number[];

This gives me an error in console:

transform[stderr]: Unable to determine type of 'validTimes' property

How should I specify such an property as array of primitive values in Realm/React native?

For clarity, do you want to have an array of numbers, or a Realm List of numbers?

Defining an array of primitive values in Realm/React Native is fairly straightforward. Let me break it down for you in a step-by-step manner.

Step 1: Import the necessary modules

Before we can define an array of primitive values, we need to import the required modules. In this case, we will need to import the Realm module.

Step 2: Create a Realm object

Next, we need to create a Realm object that will hold our array. To do this, you can use the new Realm() constructor.


import Realm from 'realm';

const realm = new Realm({ schema: [YourSchema] });

Replace YourSchema with the schema definition for your specific use case.

Step 3: Define your array

Now that we have our Realm object ready, we can define our array of primitive values. In Realm, arrays are represented using the List type. This is similar to how arrays are defined in JavaScript.


const MySchema = {

name: 'MySchema',

properties: {

myArray: 'list',

},

};

realm.write(() => {

realm.create('MySchema', {

myArray: [1, 2, 3, 4, 5],

});

});

In this example, we define a schema called MySchema with a property called myArray of type list. We then use the realm.create() method to create an instance of MySchema with the myArray property set to an array of primitive values.

Step 4: Access and manipulate the array

Once you have defined your array, you can access and manipulate its elements using standard JavaScript array methods.


const result = realm.objects('MySchema')[0];

console.log(result.myArray); // [1, 2, 3, 4, 5]

realm.write(() => {

result.myArray.push(6);

});

console.log(result.myArray); // [1, 2, 3, 4, 5, 6]

In this example, we retrieve the MySchema instance using realm.objects('MySchema')[0]. We then log the initial value of myArray. After that, we use the push() method to add an element to the array and log the updated value.

That’s it! You have successfully defined an array of primitive values in React Native.

You can also choose alternative to React Native - Flutter

Remember to adjust the code according to your specific requirements and schema definition.