我无法使用 Realm 创建列表以便将其与 Realm ListView 一起使用。
我需要制作2或3个ListView,你可以看到下面的代码 fragment :
realm.js:
const Realm = require ('realm');
class UserEntrySchema extends Realm.Object {}
UserEntrySchema.schema = {
name:'User',
primaryKey:'id',
properties:{
id:'int',
name:'string',
username: 'string',
email: 'string',
}
};
class UserListSchema extends Realm.Object {}
UserListSchema.schema = {
name:'UserList',
properties: {
users :{type:'list', objectType:'User'}
}
};
class HistorySchema extends Realm.Object {}
HistorySchema.schema = {
name : 'History',
properties :{
items : {type:'list',objectType:'User'}
}
};
export default new Realm({
schema:[UserEntrySchema,UserListSchema,HistorySchema], schemaVersion:0});
我使用以下代码创建了第一个 ListView:
export default class SearchTabScreen1 extends Component {
constructor(props) {
super(props);
this.state = {
searchText:'',
data:[]
};
}
componentDidMount(){
this.fetchUsers();
}
fetchUsers(){
console.log("FEEEEEETCH");
fetch('http://jsonplaceholder.typicode.com/users')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
data : responseJson
});
var data = this.state.data;
realm.write(() => {
for (var i = 0; i < data.length; i++){
let myUser = realm.create('User',data[i],true);
}
console.log("ALLUSER", realm.objects('User'));
});
})
.catch(function(e) { // Failure callback registartion
alert('Failure fetching data');
console.log(e)
});
}
我尝试使用
创建列表属性UserListSchema
没有成功。
目前,即使我使用此 ListView 也可以正常工作
realm.objects('User')
作为数据源。
我不知道这样做好不好。
第二个ListView是“搜索历史记录”,当单击第一个ListView的一行时,它调用以下方法来推送另一个屏幕(我正在使用react-native-navigation包)并填充 Realm 列表。我想使用此列表作为“历史 ListView”的数据源。
onPushPress(rowData) {
this.props.navigator.showModal({
title: "Random Title",
screen: "Project.WordDefinition",
passProps: {
definition: rowData.name
}
});
realm.write(() => {
let historyList = realm.create('History',{},true);
historyList.items.push({rowData});
});
}
}
我收到此错误:
'Property' must be of type number
我也尝试过:
onPushPress(rowData) {
console.log("ROWDATA", rowData);
this.props.navigator.showModal({
title: "Titre",
screen: "AccessiDico.WordDefinition",
passProps: {
definition: rowData.name
}
});
realm.write(() => {
let historyList = realm.create('History',{},true);
historyList.items.push({
id:rowData.id,
name:rowData.name,
username: rowData.username,
email: rowData.email,
});
console.log("ROWDATA",rowData);
console.log("ITEMS",realm.objects('History'));
console.log("List",historyList.items);
});
}
}
我得到了这个错误:
Attempting to create an object of type 'User' with an existing primary key value
这是否意味着我无法在“UserEntrySchema”中使用“我的用户”将它们推送到 Realm 列表中?
我真的很感激一些帮助,这一周我一直在努力解决这个问题:+1:
谢谢!
PS:历史ListView的编码方式如下:
export default class HistoryTabScreen2 extends Component {
constructor(props) {
super(props);
// if you want to listen on navigator events, set this up
//this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
postDataSource: ds.cloneWithRows(realm.objects('History')),
};
}
renderRow(rowData, sectionId, rowId, highlightRow){
return(
<TouchableHighlight onPress={this.onPushPress.bind(this)}>
<View style={styles.row}>
<Text style={styles.rowText}>{rowData.name}</Text>
</View>
</TouchableHighlight>
)
}
render(){
return(
<ListView
dataSource={this.state.postDataSource}
renderRow={this.renderRow.bind(this)}
/>
);
}
以及当我单击 ListView 的一行时我的 rowData 的日志:
'ROWDATA', { id: 5,
name: 'Chelsey Dietrich',
username: 'Kamren',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="125e67717b7d4d5a7766667b7c75776052737c7c7b773c7173" rel="noreferrer noopener nofollow">[email protected]</a>',
address:
{ street: 'Skiles Walks',
suite: 'Suite 351',
city: 'Roscoeview',
zipcode: '33263',
geo: { lat: '-31.8129', lng: '62.5342' } },
phone: '(254)954-1289',
website: 'demarco.info',
company:
{ name: 'Keebler LLC',
catchPhrase: 'User-centric fault-tolerant solution',
bs: 'revolutionize end-to-end systems' } }
最佳答案
我刚刚回答了有关列表对象的问题。 查看此链接,然后在需要时要求进一步说明。在文章的末尾,我提供了一个从 Realm 列表对象创建/修改/附加/删除对象的工作示例。 How can I properly copy objects from one Realm object to another object
创建列表非常简单,这是最简单的情况:
class Letters: Object {
var letterList = List<Letter>()
}
因此,要创建列表,您需要知道将使用的 Object 子类。 (上例中的字母)。
要添加到列表中,您需要创建一个对象(项目 - 下面),然后附加到列表中:
firstList.letterList.append(item)
关于android - Realm 的列表属性如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43736727/