如何使用异步提取请求使用领域



我满足了我的要求:

步骤1.将数据保存到手机(Realm)中的本地DB

步骤2.将本地数据上传到服务器,如果成功,服务器将返回数据ID步骤3.通过返回的ID删除本地数据库中的记录,该记录通过步骤2

获得
Realm.open({schema:[MySchame],encryptionKey:getRealmKey()})
        .then(realm =>{
            realm.write(() => {
                // 1. get all step data from db
                let objetcs = realm.objects('MySchema');
                // 2. upload obtained data to server
                if(objetcs.length > 0){
                    let recordArr = [];
                    for (let o of steps){
                        recordArr.push(o.get());
                    }
                   uploadDataToServer(recordArr,(res)=>{
                        //3. filter the uploaded steps and delete them
                        let uploadedSteps = realm.objects('MySchema').filtered('id=$0',res.id);
                        if(uploadedSteps.length > 0){
                            realm.delete(uploadedSteps);
                        }
                    });
                }
            });
            realm.close();
        })
        .catch(error =>{
            console.log(error);
        });

但这并非预期,似乎DB已关闭过早,而不是Networks成功回调。感谢任何想法。

最后,我使用这样的领域:

let realm = new Realm({schema:[JOB_SCHEMA.jobTrack],encryptionKey:getRealmKey()});
        let objects = realm.objects('JobTrack');
        realm.beginTransaction();
        realm.delete(objects);
        realm.commitTransaction();
        realm.close();

首先创建一个类似于下面的服务

import repository from "./realmConfig";
let CatalogsService = {
    findAll: function () {
        return repository.objects("CatalogsModel");
    },
    save: function (catalogs) {
        repository.write(() => {
            repository.create("CatalogsModel", catalogs);
        });
    },
    delete: function () {
        repository.write(() => {
            let all = repository.objects("CatalogsModel");
            repository.delete(all);
        });
    },
    update: function (catalogs, callback) {
        if (!callback) return;
        repository.write(() => {
            callback();
            catalogs.updatedAt = new Date();
        });
    }
};
module.exports = CatalogsService;

我的realmConfig文件为

import Realm from "realm";
class CatalogsModel extends Realm.Object { }
CatalogsModel.schema = {
    name: "CatalogsModel",
    primaryKey: "id",
    properties: {
        id: "string",
        name: "string",
        url: "string",
        status: "int"
    }
};
class OffersModel extends Realm.Object { }
OffersModel.schema = {
    name: "OffersModel",
    primaryKey: "id",
    properties: {
        id: "string",
        name: "string",
        url: "string",
        status: "int",
        machineId: "string",
        machineName: "string"
    }
};
export default new Realm({
    schema: [CatalogsModel, OffersModel],
    schemaVersion: 1,
    deleteRealmIfMigrationNeeded: true
});

现在导入Service.js您调用async服务器调用并完成工作。有关参考,请参见下面的代码

import CatalogService from './path/to/CatalogService .js'
//get objects
var catalogs = CatalogsService.findAll();
// fire async function , I prefer axios for network calls
Axios.post("SERVER_URL", {
        data: catalogs
    })
    .then(function (response) {
        if (response.success)
            CatalogsService.delete()
    }

我假设您可以根据需要轻松修改findAll()delete()方法

相关内容

  • 没有找到相关文章

最新更新