I want to store 100000 record in my application so i can use it in offline mod first I try to use Asyncstorage
try{
await AsyncStorage.setItem(@Devices, JSON.stringify(data));
}
but I have problem when I try to get the saved data
let Devices = await AsyncStorage.getItem('@Devices');
let newList = await JSON.parse( Devices );
AsyncStorage throw error :
AsyncStorage Couldn't read row 0, col 0 from CursorWindow
then I try to use react-native-sqlite-storage I face problem where
I have a problem that the data must be added one by one (for loop to insert 100000 record) and this led to the application being stuck
// Didmount
useEffect(() => {
createTable();
});
const createTable = () => {
db.transaction(tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS'
'USERS'
'(ID INTEGER PRIMARY KEY AUTOINCREMENT,item_code TEXT,item_desc TEXT,ser_id TEXT,inv_no TEXT,status TEXT,location TEXT,)',
);
});
};
const insertIntoTable = () => {
deviceList.map(item=>{
db.transaction(tx => {
tx.executeSql(
'INSERT INTO USERS (item_code,item_desc,ser_id,inv_no,status,location) VALUES (?,?,?,?,?,?,)',
[item.item_code, item.item_desc, item.ser_id, iitem.nv_no, item.status, item.location],
);
});
})
};
CodePudding user response:
you have to call transaction at first ,and define loop in , it's something like this :
try {
db.transaction(
tx => {
deviceList.forEach(async item => {
await tx.executeSql(
'INSERT INTO USERS (item_code,item_desc,ser_id,inv_no,status,location) VALUES (?,?,?,?,?,?,)',
[item.item_code, item.item_desc, item.ser_id, iitem.nv_no, item.status, item.location],
);
});
},
null,
null,
);
} catch (error) {
result = false;
}
maybe it's Helpful :)
CodePudding user response:
I solved my problem by dividing the data into 10,000 for each one, so the 100,000 divided into 10 AsyncStorage
let loop = (await res.items.length) / 10000;
let loop2 = (await loop) % 1 === 0 ? loop : parseInt(loop) 1;
let first = -10000;
let second = 0;
let last = res.items.length;
await AsyncStorage.setItem('loop', loop2.toString());
for (var i = 0; i < loop2; i ) {
first = first 10000;
second = i 1 == loop2 ? last : second 10000;
let data = await res.items.slice(first, second);
try {
let Device = await ('@Device' i 1);
await AsyncStorage.setItem(Device, JSON.stringify(data));
if (i 1 == loop2) {
setIsLoading(false);
}
