1

我的要求符合这种情况:</p>

步骤1.将数据保存到手机(领域)中的本地数据库

step 2. 上传本地数据到服务器,成功后服务器返回数据 id step 3. 根据step2返回的id删除本地db中的记录

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 比网络成功回调关闭得太早。感谢您的任何想法。

4

2 回答 2

1

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

let realm = new Realm({schema:[JOB_SCHEMA.jobTrack],encryptionKey:getRealmKey()});
        let objects = realm.objects('JobTrack');
        realm.beginTransaction();
        realm.delete(objects);
        realm.commitTransaction();
        realm.close();
于 2018-12-24T10:02:33.403 回答
0

首先创建一个像下面这样的服务

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()方法

于 2018-12-17T10:57:41.703 回答