最开始项目里使用存储用的是realm
但现在我毅然决然的要把它们都换成storage
主要原因还是因为对realm使用的熟练度不如storage 😂
项目有这样的结构
const PubActDataSchema = {
name: 'PubActData',
primaryKey: 'cid',
properties: {
cid: 'string',
actImages: 'string[]',
actTitle: 'string',
typeStr: 'string',
actType: {type: 'list', objectType: 'ActType'},
actStartTime: 'string',
actEndTime: 'string',
actDeadlineTime: 'string',
actSearchPlace: {type: 'list', objectType: 'SearchPlace'},
actAggregateTime: 'string',
actVehicle: {type: 'list', objectType: 'Vehicle'},
actMaxNum: 'string',
actMiniNum: 'string',
actEquipment: {type: 'list', objectType: 'Equipment'},
actWhetherOpen: {type: 'list', objectType: 'OpenAct'},
allowSwitch: 'int',
createSwitch: 'int',
actCostInfo: {type: 'list', objectType: 'CostInfo'},
actIntroduce: 'string',
actRegisterNotice: 'string'
}
};
const PubActAllDataPath = 'pubActAllData.realm';
/**
* 活动类型
*/
const ActTypeSchema = {
name: 'ActType',
primaryKey: 'id',
properties: {
id: 'int',
type: 'string',
}
};
/**
* 集合位置
*/
const SearchPlaceSchema = {
name: 'SearchPlace',
primaryKey: 'id',
properties: {
id: 'int',
locateName: 'string',
latitude: 'string',
longitude: 'string',
}
};
/**
* 车辆
*/
const VehicleSchema = {
name: 'Vehicle',
primaryKey: 'id',
properties: {
id: 'int',
type: 'string',
displacement: 'string',
drive: 'string',
vehicleStr: 'string',
inputText: 'string',
}
};
/**
* 装备
*/
const EquipmentSchema = {
name: 'Equipment',
primaryKey: 'id',
properties: {
id: 'int',
textArr: 'string[]',
foodArr: 'string[]',
dailyArr: 'string[]',
drugsArr: 'string[]',
carUseArr: 'string[]',
equipmentArr: 'string[]',
equipmentStr:'string',
inputText: 'string',
}
};
/**
* 公开Bool
*/
const OpenActSchema = {
name: 'OpenAct',
primaryKey: 'id',
properties: {
id: 'int',
text: 'string',
}
};
/**
* 费用信息
*/
const CostInfoSchema = {
name: 'CostInfo',
primaryKey: 'id',
properties: {
id: 'int',
costStr: 'string',
inputText: 'string',
refund: 'string',
payment: 'string',
costData:{type: 'list', objectType: 'CostData'},
}
};
/**
* 费用明细
*/
const CostDataSchema = {
name: 'CostData',
primaryKey: 'id',
properties: {
id: 'int',
cost_name: 'string',
cost: 'string',
}
};
const PubActAllSchema = [
PubActDataSchema,
ActTypeSchema,
VehicleSchema,
EquipmentSchema,
OpenActSchema,
CostDataSchema,
CostInfoSchema,
SearchPlaceSchema,
]
export default {
PubActAllSchema,
PubActAllDataPath,
}
可以看到是嵌套的 最多三层
因为需求 使用时有时要清空存储或者创建新的
我是这么操作的
Realm.open({
path: this.realmPath,
schema: this.schema
}).then(realm => {
realm.write(() => {
let PubActData = realm.objects('PubActData').filtered(`cid = "${this.state.cid}"`)[0];
if (PubActData.actVehicle[0]) {
PubActData.actVehicle[0].type = '';
PubActData.actVehicle[0].displacement = '';
PubActData.actVehicle[0].drive = '';
PubActData.actVehicle[0].vehicleStr = '';
PubActData.actVehicle[0].inputText = '';
}
PubActData.actEquipment[0].textArr = [];
PubActData.actEquipment[0].foodArr = [];
PubActData.actEquipment[0].dailyArr = [];
PubActData.actEquipment[0].drugsArr = [];
PubActData.actEquipment[0].carUseArr = [];
PubActData.actEquipment[0].equipmentArr = [];
PubActData.actEquipment[0].equipmentStr = '';
PubActData.actEquipment[0].inputText = '';
if (PubActData.actCostInfo[0]) {
PubActData.actCostInfo[0].costStr = '免费';
PubActData.actCostInfo[0].inputText = '';
PubActData.actCostInfo[0].refund = '活动开始前均可退费';
PubActData.actCostInfo[0].payment = '线下支付';
PubActData.actCostInfo[0].costData = [];
}
PubActData.actImages = [];
PubActData.actTitle = '';
PubActData.typeStr = '';
PubActData.actType = PubActData.actType;
PubActData.actStartTime = '';
PubActData.actEndTime = '';
PubActData.actDeadlineTime = '';
PubActData.actSearchPlace = PubActData.actSearchPlace;
PubActData.actAggregateTime = '';
PubActData.actVehicle = PubActData.actVehicle;
PubActData.actMaxNum = '';
PubActData.actMiniNum = '';
PubActData.actEquipment = PubActData.actEquipment;
PubActData.actWhetherOpen = [];
PubActData.allowSwitch = 1;
PubActData.createSwitch = 1;
PubActData.actCostInfo = PubActData.actCostInfo;
PubActData.actIntroduce = '';
PubActData.actRegisterNotice = '1、用车请牢记交通规则,安全驾车。' +
'\n2、请游客注意安全,切记私自脱队或改变行程;' +
'\n3、提示:旅游人数较多,出现交通拥堵、餐厅排队等情况请用户见谅,根据领队要求,请注意集合时间,不要单独行动,出行时请保管好个人物品,不要佩戴黄金等贵重首饰,并注意人身安全。' +
'\n4、凡患有传染性疾病,心血管病,脑血管疾病,呼吸系统疾病,精神病,大型手术恢复期病患,行动不便者,孕妇等其他不适合出行者请勿报名,或他人代报名时请先确认代报名人的身体状况,否则后果自负,谢谢合作;' +
'\n5、户外活动中若遇到不可抗力因素,包括自然灾害(地震,火山,海啸,台风,冰雹,泥石流等导致道路无法通行)、政府行为(征用,管制等)、社会异常事件(罢工,骚乱,瘟疫等)不能按照行程中履行或无法按照计划实施,活动组织方有权视情况而定对线路做出调整以保证安全,但不承担任何责任,请遵守出行规范,保障个人人身安全;' +
'\n6、游客在户外活动时,对于活动中可能出现的意外事故,属于不可预知的安全事故,由投保公司承担意外伤害保险赔付,组织方不为此承担责任。'
});
this.refreshAction ? this.refreshAction() : null;
if (this.state.source === 'activity_info') {
this.props.navigation.goBack()
} else {
this.props.navigation.navigate('ClubInfoListPage');
}
});
要把一层一层都赋空值😂 感觉好麻烦 说到底还是不会用 我没找到简便赋空的方法😢
用了clear 不好用 因为realmPath是不变的 clear完之后 相同的realmPath就不让我再创建一个表了 好嚣张 没招 只能按上面一点点赋空值了😂
在看里面的层级赋值操作
realm.write(() => {
let dic = {
id: 0,
costStr: costStr,
inputText: '',
refund: '活动开始前均可退费',
payment: '线下支付',
costData: []
};
if (PubActData.actCostInfo[0]) {
PubActData.actCostInfo[0].costStr = dic.costStr;
PubActData.actCostInfo[0].inputText = dic.inputText;
PubActData.actCostInfo[0].refund = dic.refund;
PubActData.actCostInfo[0].payment = dic.payment;
PubActData.actCostInfo[0].costData = dic.costData;
}else {
PubActData.actCostInfo.push(dic)
}
});
需要一个一个赋值 不会用字典一次赋值完😂 水平有限
还有就是对于textinput赋值state指向的realm值时需要如下操作
onChangeText={(text) => {
Realm.open({
path:this.realmPath,
schema:this.schema
}).then(realm => {
realm.write(() => {
this.state.costData[index].cost_name = text;
this.setState({})
})
})
}}
除了onChangeText textInput别的方法都试了 不好使。。。
因为在componentWillMount时做了如下操作
componentWillMount() {
Realm.open({
path:this.realmPath,
schema:this.schema
}).then(realm => {
let PubActData = realm.objects('PubActData').filtered(`cid = "${this.state.cid}"`)[0];
let costInfo = PubActData.actCostInfo[0];
if (costInfo) {
this.state.textArr.map((item, index) => {
item.text === costInfo.costStr ? item.isSelect = true : item.isSelect = false;
});
this.setState({
refundText: costInfo.refund ? costInfo.refund : '活动开始前均可退费',
inputText: costInfo.inputText,
paymentText: costInfo.payment ? costInfo.payment : '线下支付',
})
}
if (costInfo.costData.length){
for (let i = 0; i< costInfo.costData.length; i++){
this.state.costData.push(costInfo.costData[i]);
}
this.setState({costData:this.state.costData, maxId:costInfo.costData[costInfo.costData.length -1].id})
}
});
}
所以毅然决然的使用storage了
再见realm 👋 有机会再见🙋♂️
网友评论