2017-11-06 2 views
0

저장하려는 여러 webAPI 메소드와 테이블이있을 때 반응 네이티브 (프로젝트 구조)에서 영역을 구현하는 가장 좋은 방법은 무엇입니까?React Native Realm 프로젝트 구조

나는이 접근 방식이 올바른지 아닌지 다음과 같이 설명합니다. 친절하게 제안하거나 링크를 제공하지 않는 경우.

  1. 는 모델 폴더를 생성 - 각이 폴더에 파일이 RealmObject로 확장 모델 클래스가 JS.

  2. 비동기 작업을위한 폴더 생성 -이 폴더의 각 js 파일 웹 API를 호출하고 Realm 개체에 데이터를 씁니다. 모든 webapi 함수에는 sperate js 파일이 있습니다.

  3. 처음에 앱이 구성 요소를로드 할 때 모든 중요한 비동기 작업을 호출하고 필요할 때마다 나머지 부분을 호출합니다.

이렇게하면 영역의 데이터를 자동으로 업데이트 할 수 없습니다. 즉, 영역 결과가 동적이 아니므로 변경 사항을 수동으로 호출하여 업데이트 된 데이터를 표시해야합니다.

+0

가능한 중복 : // howovertoflow.com/questions/40195371/how-to-organize-react-native-with-realm-project-files) –

답변

1

이 가능한 구조 :

나는 영역의 문서 사용을 제안합니다 ->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를 사용해야합니다 (() => {})

이 도움이 :) 희망

[영역 프로젝트 파일과 기본 반작용 구성하는 방법?] (HTTPS의