我的要求符合这种情况:</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 比网络成功回调关闭得太早。感谢您的任何想法。