realm / realm-js

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

Realm Database with Redux #2170

Closed benhartouz closed 5 years ago

benhartouz commented 5 years ago

Goals

In fact, I'm building a react native application and i'm using Realm as database and also I'm using redux to manage state, but I find a problem when I pass realm object through reducers my application goes slow.

Expected Results

Getting and parsing data

Actual Results

Slower navigation of react native application

Code Sample

  let users = realm.objects("User");
  dispatch({
            type: FETCH_SEARCH_USERS_GLOBAL,
            payload: users
   })

Version of Realm and Tooling

ivanpagac commented 5 years ago

You should pass only raw objects to reducers. Working with database should be done either in middleware or saga. For example in our app when loading users, i load them in saga, then dispatch action with already pulled data that is handled by reducers layer.

benhartouz commented 5 years ago

@ivanpagac thanks for your comment, in fact, I passed it through redux-thunk. what do you mean by v raw objects?

esutton commented 5 years ago

@ivanpagac Thank you for your suggestion. Please elaborate. I am not familiar with saga.

Raw objects are not realm database connected objects?

I am using database objects in my reducer and things are slow.

    "react-native": "0.55.4",
    "react-native-router-flux": "4.0.0-beta.27",
    "react-redux": "^5.0.7",
    "realm": "^2.2.14",
    "redux": "^3.7.2",
    "redux-thunk": "^2.3.0",
benhartouz commented 5 years ago

@esutton can provide a sample of your code, Now we are building the application that gets data using API or Local database, but we have troubles with Realm object when we passed to store. In fact, I did some hack to my code, I passed a callback to my action to get the result of realm.


export const fetch_wokrspaces = (cb = () => {}) => {
  return (dispatch, getState) => {
        let con = true; 
        if(con){
             // get data from api using axios
        }else{
              let users = realm.objects("User");
              cb(users);
        }
  }
};
reyalpsirc commented 5 years ago

@benhartouz I’m also using Realm and Redux on projects and in my case, I use the thunks/sagas to write API responses to the Realm database only and, on the Containers, I just make the queries to the Realm database and make them listen for live changes. With this behavior, I only use Redux for stuff that I don’t need to store on device and which is shared along different Containers.

kneth commented 5 years ago

https://github.com/lolatravel/realm-react-redux is not supported or endorsed by us but might be worth a try.

realm-probot[bot] commented 5 years ago

Hey - looks like you forgot to add a T:* label - could you please add one?