realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.79k stars 576 forks source link

Retrieve data and close realm =>Error: Transaction_ended #6472

Closed VikasReactNative closed 7 months ago

VikasReactNative commented 9 months ago

How frequently does the bug occur?

Always

Description

const realm = Realm.open({ schema: [ PlaceSchema, LocationSchema, ContactInformationSchema, EditorialSummarySchema, ReviewSchema, PhotoSchema, AuthorAttributionSchema, TextSchema, displayNameSchema, ], deleteRealmIfMigrationNeeded: true, }); try { const places = realm.objects('Place'); // Retrieve all 'Place' objects const placesArray = Array.from(places); console.log('Data fetched from Realm:', placesArray); return placesArray; } catch (error) { console.error('Error fetching data from Realm:', error); } finally { realm.close(); }

expected realm should close but it throw error

Error: Transaction_ended and app crash

and some times=> Error: List is no longer valid. Either the parent object was deleted or the containing Realm has been invalidated or closed.

Stacktrace & log output

Error: transaction_ended

This error is located at:
    in Unsorted
    in RCTView (created by View)
    in View (created by Sort)
    in AndroidHorizontalScrollContentView (created by ScrollView)
    in AndroidHorizontalScrollView (created by ScrollView)
    in ScrollView (created by ScrollView)
    in ScrollView (created by Sort)
    in Sort (created by Connect(Sort))
    in Connect(Sort) (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by SceneView)
    in RCTView (created by View)
    in View (created by SceneView)
    in SceneView (at TabView.tsx:115)
    in RCTView (created by View)
    in View (created by PagerView)
    in RNCViewPager (created by PagerView)
    in PagerView (created by AnimatedComponent)
    in AnimatedComponent
    in AnimatedComponentWrapper (created by PagerViewAdapter)
    in PagerViewAdapter (created by TabView)
    in RCTView (created by View)
    in View (created by TabView)
    in TabView (created by MaterialTopTabView)
    in MaterialTopTabView (created by MaterialTopTabNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by MaterialTopTabNavigator)
    in MaterialTopTabNavigator (created by TopNavigation)
    in TopNavigation (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by CardContainer)
    in RCTView (created by View)
    in View (created by CardContainer)
    in RCTView (created by View)
    in View (created by CardContainer)
    in RCTView (created by View)
    in View
    in CardSheet (created by Card)
    in RCTView (created by View)
    in View (created by AnimatedComponent)
    in AnimatedComponent
    in AnimatedComponentWrapper (created by PanGestureHandler)
    in PanGestureHandler (created by PanGestureHandler)
    in PanGestureHandler (created by Card)
    in RCTView (created by View)
    in View (created by AnimatedComponent)
    in AnimatedComponent
    in AnimatedComponentWrapper (created by Card)
    in RCTView (created by View)
    in View (created by Card)
    in Card (created by CardContainer)
    in CardContainer (created by CardStack)
    in RNSScreen (created by AnimatedComponent)
    in AnimatedComponent
    in AnimatedComponentWrapper (created by InnerScreen)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze (created by InnerScreen)
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by CardStack)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by CardStack)
    in RCTView (created by View)
    in View (created by Background)
    in Background (created by CardStack)
    in CardStack (created by HeaderShownContext)
    in RNCSafeAreaProvider (created by SafeAreaProvider)
    in SafeAreaProvider (created by SafeAreaInsetsContext)
    in SafeAreaProviderCompat (created by StackView)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by StackView)
    in StackView (created by StackNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by StackNavigator)
    in StackNavigator (created by Stack)
    in Stack (created by RootNavigation)
    in RootNavigation (created by Connect(RootNavigation))
    in Connect(RootNavigation) (created by App)
    in Provider (created by App)
    in EnsureSingleNavigator
    in BaseNavigationContainer
    in ThemeProvider
    in NavigationContainerInner (created by App)
    in App
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in tripsquee(RootComponent), js engine: hermes

Can you reproduce the bug?

Always

Reproduction Steps

just read data from real and close request =>Error: transaction_ended

Version

12.5.0

What services are you using?

Atlas App Services: Functions or GraphQL or DataAPI etc

Are you using encryption?

No

Platform OS and version(s)

android

Build environment

Which debugger for React Native

Cocoapods version

No response

kneth commented 9 months ago

My guess is that your data model has links, and Array.from() does not copy deeply. It might be better to use the spread operator if you really need to copy the objects.

VikasReactNative commented 9 months ago

i have tried but having same issue

realm = await openRealm(); const response = realm.objects('Place'); const businesses = [...response]; // Array.from(response); if (Array.isArray(businesses) && businesses.length > 0) { const formattedAddressWords = businesses[0]?.formattedAddress?.length > 0 && businesses[0]?.formattedAddress?.split(' '); const lastWord = formattedAddressWords[formattedAddressWords.length - 1]; dispatch(setLocation(lastWord)); fetchAllPlacesDetails(businesses); dispatch(setPlacesDataLength(businesses.length)); realm.close()

kneth commented 9 months ago

The issue is that neither Array.from() or ... will create true POJOs, and once you close the Realm (realm.close()), any managed (Realm) objects will be invalidated.

The following code snippet illustrates it:

const Realm = require("realm");

const A = {
  name: "A",
  properties: {
    a: "int",
  },
};

const B = {
  name: "B",
  properties: {
    a: "A",
    b: "int",
  },
};

let realm = new Realm({ schema: [A, B] });
realm.write(() => {
  realm.create("B", { b: 11, a: { a: 22 } });
});

let bs = realm.objects("B");
let pojos1 = [...bs];
console.log(`Before close: ${pojos1[0] instanceof Realm.Object} ${pojos1[0].a instanceof Realm.Object}`);

let pojos2 = bs.toJSON();
realm.close();
console.log(`After close: ${pojos2[0] instanceof Realm.Object} ${pojos2[0].a instanceof Realm.Object}`);

If you need to copy deeply from Realm, toJSON() is your best option.

github-actions[bot] commented 7 months ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.