Hamza Qayyum
Hamza Qayyum

Reputation: 1

Realm local data base is very slow when writing and reading big objects/ React Native App Hangs

I am trying to write data in realm that i am getting from a BLE listener. 6000 objects in 10 to 20 seconds i tried to write all the objects in realm but it was very slow and taking much more time so i tried to write bulk of 200 objects but it is still taking much time and React Native App hangs when writing data and when reading data from Realm. I am looking for solution

This is where getting data from the BLE listener and writing data in Realm

let a = [];
  let arrayBuffer = [];
  const handleNotificationUpdate = data => {
    if (data) {
      let byteArray = data.value;
      const hexArray = byteArrayToHexArray(byteArray);
      const packets = separatePackets(hexArray);
      const decimalArrays = packets.map(packet => packet.map(hex => parseInt(hex, 16)));
      const extractedValues = extractValuesFromPackets(decimalArrays);
  
      // Adding activityType and currentTime to objects
      const currentDate = new Date();
      const options = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: true,
        timeZone: 'Asia/Karachi', // Pakistan timezone
      };
      const currentTime = currentDate.toLocaleString('en-PK', options);
  
      extractedValues.forEach(obj => {
        if (!obj.hasOwnProperty('activityType')) {
          obj.activityType = 1;
        }
        obj.currentTime = currentTime;
      });
  
      // Filter out objects with activityType undefined
      const filteredValues = extractedValues.filter(obj => obj.activityType !== undefined && obj.packetEndHeader !== undefined);
  
      arrayBuffer.push(filteredValues);
      handleArrayBuffer();
      a = filteredValues;
      console.log(arrayBuffer.length);
    }
  };
  
  const handleArrayBuffer = () => {
    if (arrayBuffer.length === 200) {
      console.log('000000000');
      // console.log('data inside arraybuffer------->',arrayBuffer);
      insertDataIntoRealm(arrayBuffer);
      arrayBuffer = [];
    }
  };

This is my Custom File for Realm

import Realm from 'realm';

const Array2Schema = {
 name: 'Array2',
 properties: {
   activityType: 'int',
   currentTime: 'string',
   greenLed1Counts: 'int[]',
   greenLed2Counts: 'int[]',
   irLedCounts: 'int[]',
   outputFormat: 'int',
   packetEndHeader: 'int',
   packetStartHeader: 'int',
   redLedCounts: 'int[]',
   temperatureSensor: 'int[]',
   time: 'int[]',
   xAxisReading: 'int[]',
   yAxisReading: 'int[]',
   zAxisReading: 'int[]',
 },
};

const Array1Schema = {
 name: 'Array1',
 properties: {
   array2: 'Array2[]',
 },
};

const openRealm = async () => {
 return Realm.open({
   schema: [Array2Schema, Array1Schema],
   schemaVersion: 1,
 });
};

const insertDataIntoRealm = async (arrayData) => {
 try {
   const realm = await openRealm();

   realm.write(() => {
     arrayData.forEach((array2Items) => {
       const array1 = realm.create('Array1', {});
       array2Items.forEach((item) => {
         const array2 = realm.create('Array2', item);
         array1.array2.push(array2);
       });
     });
   });

   realm.close();
 } catch (error) {
   console.log('Error:', error);
 }
};

export { insertDataIntoRealm, openRealm };

Upvotes: 0

Views: 113

Answers (0)

Related Questions