database - React Native [iOS] 数据库选项

标签 database sqlite reactjs react-native asyncstorage

我希望在 React Native 中创建一个 iOS 应用程序,并且想知道什么是最适合我的应用程序的数据库选项。

该应用程序将支持约 300 个数据对象,这些数据对象将从远程服务器作为 json 对象获取。这 300 个对象的属性存在一些差异。因此,我对于建立一个不灵活的数据库模式犹豫不决。

理想情况下,我只想要 1 个具有 2 个属性的数据库: 1)id(例如:从1到300) 2)数据(例如:{"hello": "world"})

给出以上内容,我应该使用哪种react-native数据库模块有什么建议吗?

谢谢

最佳答案

根据我自己之前成功的react-native项目的经验,你可以使用AsyncStorage,它简单但足够强大,你可以存储任何你想要的东西!

此外,您还应该使用 fluxredux,它们将为您提供一个Store解决方案,您可以在其中读取和存储相关数据轻松地AsyncStorage(每个地方,每个页面)!

步骤是(主流程关于如何组织数据和构建逻辑的想法):

0/导入库:

import React, { Component } from 'react';
import {
  AsyncStorage,
  // ...
} from 'react-native';

1/从 API 或其他地方(本地文件等)获取数据,然后将数据写入(保存)到 AsyncStorage:

async firstWriteFavorite() {
    fetch("YOUR_API_URL", {method: "GET", headers: {'Cache-Control': 'no-cache'}})
        .then((response) => response.json())
        .then((responseJson) => {
            try {
                // make sure you write a STRING into AsyncStorage, 
                // and also be careful which key of the JSON should be written, the below line is just a good example:
                await AsyncStorage.setItem("@PROJECT_NAME:your_favorite_array", JSON.stringify(responseJson.response));
                // if you use flux or redux here, you can perform some action here, then you can load the data everywhere later:
                // FavoriteActionCreators.set_favorite(responseJson.response);
            } catch (error) {
                console.log('AsyncStorage error: ' + error.message);
            }   
        })
        .catch((error) => {
            console.log("Error in first getting ajax data!", error);
        }
    );  

}   

2/AsyncStorage 检索数据:

async loadFavorite() {
    try {
        var fav_array_string = await AsyncStorage.getItem("@PROJECT_NAME:your_favorite_array");
        // the above returned value is a STRING, then you can split it, or do whatever based on the structure you have written  
        var real_fav_id_array = fav_array_string.split('YOUR_SEPARATOR');
        // ...
    } catch (error) {
        console.log('AsyncStorage error: ' + error.message);
    } 
}               

3/当需要更新数据时,首先检索数据,然后将数据绑定(bind)到变量并更改该变量,然后将新数据写入异步存储:

async saveFavorite() {
    // loadFavorite() here, 
    // make sure you've got data "your_new_JSON_data" which has been converted into object, then maybe: "your_new_JSON_data.push({NEW_OBJ})";
    // after that, SAVE NEW DATA now:
    try {
        await AsyncStorage.setItem("@PROJECT_NAME:your_favorite_array", JSON.stringify(your_new_JSON_data));
        // same here, if you use flux or redux here, you can save the new data here:
        // FavoriteActionCreators.set_favorite(your_new_JSON_data);
    } catch (error) {
        console.log('AsyncStorage error: ' + error.message);
    }   
}

以上代码是从我的真实项目代码中复制并缩短的,如果有问题请尝试告诉我!

关于database - React Native [iOS] 数据库选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44016273/

相关文章:

mysql - 下面SQL查询的解释

sql-server - Codeigniter 3 未连接到 sqlserver 数据库

android - 遍历 SQLite 表的行以更新 ID

javascript - 你如何在 react 中调用另一个组件中的组件?

python - URL : Binary Blob, Unicode 或编码的 Unicode 字符串?

android - 在 Android 应用程序中不断更新数据库是一种好习惯吗?

sqlite - 如何防止Sqlite未绑定(bind)参数解释为NULL

sql - 选择与联结表中的许多其他ID有关系的记录

reactjs - 为什么不能根据 Prop react 设置初始状态

reactjs - 使用 Enzyme 测试 Redux 连接的组件