简体   繁体   English

带同步的Realm + React Native最佳实践用户设置

[英]Realm + React Native best practice user setup with sync

I am experimenting with integrating my app with Realm.io.. as it seems like a pretty awesome platform to use. 我正在尝试将我的应用程序与Realm.io ..集成在一起,因为它看起来像是一个很棒的平台。 I am trying to login + register users correctly through something like: 我正在尝试通过类似以下方式正确登录+注册用户:

Realm.Sync.User.register('http://realm-ip:9080', this.state.email, this.state.password, (error, user) => {
  if (!error) {
    var realm = new Realm({
      sync: {
        user: user,
        url: 'realm://realm-ip:9080/~/userRealm',
      },
      schema: [PersonSchema, ConversationSchema]
    });
  }
  else {
    console.log(error);
  }
})

This seems to work pretty well. 这似乎工作得很好。 I have similar code for loggin a user into realm. 我有类似的代码可将用户登录到领域。 Though, when looking at the Realm JS (React Native) examples here: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js they export the realm object with something like: 但是,当在这里查看Realm JS(React Native)示例时: https : //github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js,它们会使用类似的内容导出领域对象:

 export default new Realm({schema: [Todo, TodoList]});

Now, this seems pretty neat, and makes the use of realm flawlessly in the rest of the app. 现在,这看起来很整洁,并且在应用程序的其余部分中完美地使用了realm However, it doesn't make use of the sync object with user details in it, as provided when using Realm.Sync.User.* Also I can't figure out the best practice way to achieve this modular design, but still have users login/register via the fist example I showed with Realm.Sync.User.register() 但是,它没有使用其中包含用户详细信息的sync对象,这与使用Realm.Sync.User.*时提供的Realm.Sync.User.*另外,我也无法弄清楚实现此模块化设计的最佳实践方法,但仍然有用户通过我用Realm.Sync.User.register()展示的第一个示例登录/注册

The example app here https://github.com/realm/realm-js/blob/master/examples/ReactExample doesn't give solid examples of registering users. 此处的示例应用程序https://github.com/realm/realm-js/blob/master/examples/ReactExample没有提供注册用户的可靠示例。

My questions are: 我的问题是:

  • What's the best way to keep things neat and setup users correctly? 保持整洁并正确设置用户的最佳方法是什么?
  • Is there a better way to access an initialised realm object that's been setup with sync: { user: user, url: 'realm url'} ? 有没有更好的方法来访问已通过sync: { user: user, url: 'realm url'}设置的初始化realm对象?
  • Is there a better example to learn how to do this from? 有没有更好的例子来学习如何做到这一点?

If my question doesn't make sense, please let me know and I will try to elaborate... as I realise it might not be easy to understand. 如果我的问题没有道理,请告诉我,我将尽力阐述...,因为我意识到这可能不容易理解。

Well, it kind of depends on your situation and there are a number of approaches that could work. 好吧,这取决于您的情况,并且有许多方法可以起作用。 I guess you could do something like this: 我猜你可以做这样的事情:

// realm-container.js

import Realm from 'realm';

export default {
  realm: null,
  initialize: (email, password) => {
    Realm.Sync.User.register('http://realm-ip:9080', email, password, (error, user) => {
      if (!error) {
        this.realm = new Realm({
          sync: {
            user: user,
            url: 'realm://realm-ip:9080/~/userRealm',
          },
          schema: [PersonSchema, ConversationSchema]
        });
      }
      else {
        console.log(error);
      }
    })
  }
};

Then, if you include that as import realmContainer from './realm-container'; 然后,如果您将其作为从'./realm-container'导入的realmContainer;

you can then call realmContainer.initialize(email, password) and refer to the realm like this: realmContainer.realm 然后,您可以调用realmContainer.initialize(email, password)并参考如下领域: realmContainer.realm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM