App crashes while read/write operation in react native

Hello ! IOS App crashes while i perform a write to the db , the record gets added and the app crashes .

here is the crash report for reference

crashRelam.txt (57.5 KB)

Any light is much appreciated .

1 Like

Hi @Adithya_Sundar,

Just as a reference, if you give the crash report a .ips suffix, it will be properly formatted.

It looks like the crash happens well inside the hermes Javascript engine

…
Thread 7 Crashed:: com.facebook.react.JavaScript
0   hermes                        	       0x108c18a20 0x108c14000 + 18976
1   hermes                        	       0x108e852d8 0x108c14000 + 2560728
2   hermes                        	       0x108e3a3d0 0x108c14000 + 2253776
3   hermes                        	       0x108c650f0 0x108c14000 + 332016
4   hermes                        	       0x108c592c8 0x108c14000 + 283336
5   hermes                        	       0x108c57c34 0x108c14000 + 277556
6   hermes                        	       0x108c3a728 0x108c14000 + 157480
7   hermes                        	       0x108cde9d0 0x108c14000 + 829904
8   hermes                        	       0x108c39a50 0x108c14000 + 154192
9   hermes                        	       0x108c58734 0x108c14000 + 280372
10  hermes                        	       0x108c57c34 0x108c14000 + 277556
11  hermes                        	       0x108c39cf8 0x108c14000 + 154872
12  hermes                        	       0x108c385fc 0x108c14000 + 148988
13  hermes                        	       0x108d0a564 0x108c14000 + 1008996
14  hermes                        	       0x108c39a50 0x108c14000 + 154192
15  hermes                        	       0x108c58734 0x108c14000 + 280372
16  hermes                        	       0x108c57c34 0x108c14000 + 277556
17  hermes                        	       0x108c39cf8 0x108c14000 + 154872
18  hermes                        	       0x108c39560 0x108c14000 + 152928
19  hermes                        	       0x108c58758 0x108c14000 + 280408
20  hermes                        	       0x108c57c34 0x108c14000 + 277556
21  hermes                        	       0x108c39cf8 0x108c14000 + 154872
22  hermes                        	       0x108c39560 0x108c14000 + 152928
23  hermes                        	       0x108c1e5cc 0x108c14000 + 42444
24  Paperflite                    	       0x104e2fae8 facebook::jsi::RuntimeDecorator<facebook::jsi::Runtime, facebook::jsi::Runtime>::call(facebook::jsi::Function const&, facebook::jsi::Value const&, facebook::jsi::Value const*, unsigned long) + 76 (decorator.h:303)
25  Paperflite                    	       0x104e2e198 facebook::jsi::WithRuntimeDecorator<facebook::react::(anonymous namespace)::ReentrancyCheck, facebook::jsi::Runtime, facebook::jsi::Runtime>::call(facebook::jsi::Function const&, facebook::jsi::Value const&, facebook::jsi::Value const*, unsigned long) + 88 (decorator.h:709)
26  Paperflite                    	       0x104ed1180 facebook::jsi::Function::call(facebook::jsi::Runtime&, facebook::jsi::Value const*, unsigned long) const + 100 (jsi-inl.h:234)
27  Paperflite                    	       0x104ed10fc facebook::jsi::Function::call(facebook::jsi::Runtime&, std::initializer_list<facebook::jsi::Value>) const + 112 (jsi-inl.h:239)
28  Paperflite                    	       0x104eee760 facebook::jsi::Value
…

It’s then difficult to understand what may have gone wrong, without looking at what the Javascript was doing, i.e. your code, including the proper setup of Realm before writing to the DB.

Can you share some more info on that?

Sure .

Schema setup

import Realm from 'realm'
import {createRealmContext} from '@realm/react';
import { Assets } from './schema/asset';
import { Collections } from './schema/collection';
import { Sections } from './schema/section';
import { Columns, HubLayout, Rows } from './schema/hubLayout';
import { HubSection } from './schema/hubSection';
import { Groups } from './schema/group';
import { Streams } from './schema/stream';
import { Banner, CreatedBy, Icon, StreamAnalytics, assetAnalytics, assetMetaData, assetSettings, customFields, sectionChildren } from './schema/commons';
import { GroupAssetMapping, StreamAssetMapping } from './schema/mappingTable';


export const RealmContext = createRealmContext({
  schema: [ Collections, Sections, Assets, assetAnalytics, assetMetaData, assetSettings, customFields, HubLayout, Rows, Columns, HubSection, sectionChildren, Streams, Groups, StreamAssetMapping, GroupAssetMapping, Banner, Icon, CreatedBy, StreamAnalytics ],
  deleteRealmIfMigrationNeeded: true
});

import Realm, { BSON } from 'realm';

export class HubLayout extends Realm.Object {
  static schema = {
    name: 'hubLayout',
    primaryKey: 'id',
    properties: {
      id: 'objectId',
      rows: 'rows[]',
      customLayout: 'bool',
      empty:'bool',
    },
  };
}

export class Rows extends Realm.Object {
  static schema = {
    name: 'rows',
    embedded: true,
    properties: {
      columns: 'columns[]'
    },
  };
}

export class Columns extends Realm.Object {
  static schema = {
    name: 'columns',
    embedded: true,
    properties: {
      width: 'int',
      section: 'hubSection'
    },
  };
}


import Realm from 'realm';

export class HubSection extends Realm.Object {
  static schema = {
    name: 'hubSection',
    primaryKey: 'id',
    properties: {
      id: 'objectId',
      entityId: 'string',
      title: 'string?', 
      entityType: 'string?',
      layoutStyle: 'string?',
      cardStyle: 'string?',
      showFilter: 'bool?',
      userDefined: 'bool?',
      children: 'sectionChildren?'
    },
  };
}

API call and feed api data into db

  const fetchSectionById = async id => {
    let url = buildUrl(HUB_SECTION_URL_V2, {
      path: id,
    });
    try {
      const response = await APIKit.get(url);
      let section = await response.data;
      let existingSection = sections.filtered(`entityId = "${id}"`); // returns array
      if (section && existingSection) {
        let updatedSection = updateHubSectionService(section, existingSection[0]);
          if(updatedSection) {
            updateHubLayout(updatedSection, realm)
          }
      }
    } catch (error) {
      console.log(error, 'error');
    }
  };

Model where all realm write occurs . 
import {RealmContext} from '@/db';

export const useHubModel = () => {
  const {useRealm, useQuery} = RealmContext;
  const realm = useRealm();

  const createHubLayout = ( data ) => {
    try {
      realm.write(() => {
        let createdLayout = realm.create('hubLayout', data, 'modified');
      });
    } catch (error) {
      console.log(error);
    }
  };

  const updateHubLayout = ( data ) => {
    if (data) {
      try {
        realm.write(() => {
          let createdSection = realm.create('hubSection', data, 'modified');
          console.log(createdSection, 'createdSection');
        });
      } catch (error) {
        console.log(error);
      }
    }
  };

  return {createHubLayout, updateHubLayout};
};

Hope this helps . And will attach crash report with .ips next time . thanks

Hi @Adithya_Sundar

It’s a common issue that can occur when reading or writing data in React Native. The trick is to make sure you have all the necessary dependencies installed and configured correctly.

Make sure the react-native-fetch-blob library is installed for files I/O operations, as this will alleviate any issues caused by problems with file permissions or overwriting existing files. You might also need to install an async library like redux-thunk in order to process fetch requests asynchronously.

Finally, verify that your code runs without error before running it on device by using Jest for unit tests. Taking these steps should help you troubleshoot the app crashing while read/write operation in React Native.

P.s - I’m Aliena Jose works as a app developer at Techmango, React native app development company