我使用的是Realm Native的ListView,根据他们的网站,它是基于React的ListView API的。我也在遵循Realm的ListView示例found here。
我的问题如下:我目前有一个ListView,在某些情况下需要部分重新呈现,例如,X行需要更新。但是,该函数:
rowHasChanged: function(r1, r2) {
return +r1.date !== +r2.date;
}...will总是返回false,这意味着我的ListView保持不变。
代码(在我的组件构造函数中)如下所示:
let objects = realm.objects('xxxx');
let dataSource = new ListView.DataSource({
rowHasChanged: function(r1, r2) {
return +r1.date !== +r2.date;
}
});
this.state = {
objects: objects,
dataSource: dataSource.cloneWithRows(objects)
};我还有一个更新函数,它将更新dataSource状态:
updateObject(id, date) {
let obj = getObjById(id);
realm.write(() => {
obj[0].date = date;
...
});
}一旦执行了上面的函数,rowHasChanged就会触发。但是,r1和r2是相同的,因此,它们都包含新日期。这随后意味着我的ListView不会重新呈现,并且有问题的行将包含不正确的(旧)日期。
发布于 2016-08-18 01:17:17
我目前使用它作为临时解决方案,因为我们现在添加了额外的n*2计算。
我已经添加了一个名为copyArray的新函数(由于它是shallow copying,.slice(0)将无法工作)。
我的组件中的构造函数:
let objects = this.copyArray(realm.objects('xxxx'));
let dataSource = new ListView.DataSource({
rowHasChanged: function(r1, r2) {
return +r1.date !== +r2.date;
}
});
this.state = {
objects: objects,
dataSource: dataSource.cloneWithRows(objects)
};新功能:
copyArray(array) {
let newArray = [];
for(let i = 0; i < array.length; i++) {
newArray.push({
date: array[i].date,
...
});
}
return newArray;
}更新后的函数:
updateObject(id, date) {
let obj = getObjById(id);
realm.write(() => {
obj[0].date = date;
...
});
var newArray = this.copyArray(realm.objects('xxxx'));
this.setState({
objects: newArray,
dataSource: this.state.dataSource.cloneWithRows(newArray)
});
}https://stackoverflow.com/questions/39000884
复制相似问题