我有这个反应原生应用程序,它将手机联系人导入数据库。我的手机中有大约100个联系人,这需要很长时间......就像每秒1次导入一样。我敢肯定,即使代码很简单,我也在做某个错误。
realm.write(() => {
for (let i = 0, len = contacts.length; i < len; i++) {
Contact.saveOrUpdate(contacts[i].name, contacts[i].recordID, contacts[i].phoneNumbers);
}
});
saveOrUpdate
方法:
class Contact {
static saveOrUpdate(name, recordId, phones) {
const save = () => {
let c;
try {
// try an update first
c = realm.create('Contact', {
name: name.toString(),
recordID: recordId.toString()
}, true);
} catch (e) {
// if that fails, create a new contact
console.log("Created " + recordId);
c = realm.create('Contact', {
name: name.toString(),
recordID: recordId.toString()
});
}
c.phones = [];
for (let i = 0, len = phones.length; i < len; i++) {
c.phones.push({
number: phones[i].toString()
});
}
// now run an update with the phone numbers
return realm.create('Contact', c, true);
};
return save();
}
}
我的境界架构:
const PhoneNumberSchema = {
name: 'PhoneNumber',
properties: {
number: 'string'
}
};
const ContactSchema = {
name: 'Contact',
primaryKey: 'recordID',
properties: {
recordID: 'string',
name: {
type: 'string',
indexed: true
},
phones: {
type: 'list',
objectType: 'PhoneNumber'
}
}
};
任何人都可以发现我在这里做错了100个联系人存储需要这么长时间吗?