【问题标题】:React Native Realm Project StructureReact Native Realm 项目结构
【发布时间】:2017-11-06 12:39:34
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

标签: react-native realm


【解决方案1】:

有一种可能的结构:

我会建议你使用 Doc of Realm -> https://realm.io/

有一个可能的结构: 领域.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;

使用.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(() => {})

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2018-07-08
    • 2018-08-10
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多