React Native Realm项目结构 [英] React Native Realm Project Structure

查看:322
本文介绍了React Native Realm项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您要存储多个webAPI方法和表时,在react native(项目结构)中实现领域的最佳方法是什么.

我正在执行以下操作,这是否正确?如果不是,请提出建议或提供任何链接.

  1. 创建了一个模型文件夹-该文件夹中的每个js文件都有一个使用RealmObject扩展的模型类.

  2. 创建了一个用于异步任务的文件夹-该文件夹中的每个js文件都调用Web api并在Realm对象中写入数据.每个webapi函数都有一个单独的js文件.

  3. 最初,在组件加载时加载应用程序时,将调用所有重要的异步任务,并在需要时调用其余所有任务.

这样做我无法在领域中自动更新数据,即:领域结果不是动态的,我们必须手动调用更改以显示更新的数据.

解决方案

有可能的结构:

我建议您使用领域文档-> https://realm.io/

有一个可能的结构: realm.js

import * as RealmDB from 'realm';

class Passenger extends RealmDB.Object {}
Passenger.schema = {
name: 'Passenger',
primaryKey: 'id',
properties: {
  'id'                : 'string',
  'firstname'         : { type: 'string', optional: true },
  'lastname'          : { type: 'string', optional: true },
  'birthdate'         : { type: 'int',    optional: true },
  'email'             : { type: 'string', optional: true },
  'phone'             : { type: 'string', optional: true },
  'child'             : { type: 'linkingObjects', objectType: 'Child', property: 'passengers' }
}
};


class Child extends RealmDB.Object {}
Child.schema = {
 name: 'Child',
 primaryKey: 'id',
  properties: {
   'id'                  : 'string',
   'name'                : 'string',
   'parents_1'           : { type: 'linkingObjects', objectType: 'Passenger', property: 'child' }
  }
 };


const realmInstance = new RealmDB({
schema: [Passenger, Child],
});
export default realmInstance;

use.js

import realm from "./realm";
export default class use {

 static writeToRealm(){
  realm.write(() => {

  let passenger = realm.create('Passenger', {
  'id'            : "..."
  'firstname'     : "...",
  'lastname'      : "...",
  "..."
  })
 }

 static readPassengers(){
  const passengers = realm.objects('Passengers');
  return passengers // Be careful Realm use List instead of Array quite the same but not! 
 }
}

每次要在数据库中写入时,都必须使用 realm.write(()=> {})

希望有帮助:)

What's the best way to implement realm in react native(project structure), when you have multiple webAPI methods and tables to store.

I'm doing the following is this a right approach or not? If not kindly suggest or provide any link.

  1. created a model folder - each js file in this folder have a model class extended with RealmObject.

  2. Created a folder for asynchronous tasks - each js file in this folder Calls web api and write data in Realm objects. Every webapi function has a sperate js file.

  3. Initially when app loads on component mount call all important async tasks and call rest of them wherever needed.

Doing this I'm unable to auto update data in realm i.e: Realm results are not dynamic we have to manually call on change to show the updated data.

解决方案

There a possible structure:

I will suggest you to use the Doc of Realm -> https://realm.io/

There a possible structure: realm.js

import * as RealmDB from 'realm';

class Passenger extends RealmDB.Object {}
Passenger.schema = {
name: 'Passenger',
primaryKey: 'id',
properties: {
  'id'                : 'string',
  'firstname'         : { type: 'string', optional: true },
  'lastname'          : { type: 'string', optional: true },
  'birthdate'         : { type: 'int',    optional: true },
  'email'             : { type: 'string', optional: true },
  'phone'             : { type: 'string', optional: true },
  'child'             : { type: 'linkingObjects', objectType: 'Child', property: 'passengers' }
}
};


class Child extends RealmDB.Object {}
Child.schema = {
 name: 'Child',
 primaryKey: 'id',
  properties: {
   'id'                  : 'string',
   'name'                : 'string',
   'parents_1'           : { type: 'linkingObjects', objectType: 'Passenger', property: 'child' }
  }
 };


const realmInstance = new RealmDB({
schema: [Passenger, Child],
});
export default realmInstance;

use.js

import realm from "./realm";
export default class use {

 static writeToRealm(){
  realm.write(() => {

  let passenger = realm.create('Passenger', {
  'id'            : "..."
  'firstname'     : "...",
  'lastname'      : "...",
  "..."
  })
 }

 static readPassengers(){
  const passengers = realm.objects('Passengers');
  return passengers // Be careful Realm use List instead of Array quite the same but not! 
 }
}

Every time you want to write in your database you have to use realm.write(() => {})

Hope it's help :)

这篇关于React Native Realm项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆