rn redux-offline 删除请求 data/cache
rn redux-offline delete request data/cache
刚刚卡在配置 react-native 离线请求中。我想用它来发送数据和图像文件作为 base64 数组,好吧,我让它工作了,但在那之后缓存不断增长,每天 140mb/4h,同时在真实设备中进行测试,这假设不是它应该工作的方式。我一般缺少什么?
使用自定义请求存储配置(将图像转换为 base64)
offlineConfig2 = {
...offlineConfig,
persistCallback: () => {
console.log('Rehydratation complete');
},
persistOptions: {
key: 'primary',
storage:AsyncStorage
},
rehydrate: true,
// @overwrite effect
effect: (effect, action) => {
console.log(`Executing effect for ${action.type}:`, effect);
photoArr = [];
if (effect.photo)
photoArr = effect.photo;
promises = photoArr.map(function(photo){
return base64 = RNFetchBlob.fs.readFile(photo.key, 'base64')
.then((data) => {
return data
}).catch(err => console.log(err));
});
return new Promise(
function (resolve, reject) {
Promise.all(promises).then(function(base64Photo) {
body = effect.body;
if(Array.isArray(base64Photo) && base64Photo.length > 0) {
body = Object.assign({photoArr: base64Photo}, body);
}
fetch(effect.url, { // eslint-disable-line
method: effect.method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer' + effect.token
},
body: JSON.stringify(body),
})
.then(res => {
console.log('response', res);
resolve('ok');
return res.ok
? res.json()
: Promise.reject(res.text().then(msg => new Error(msg)));
})
.catch((error) => {
reject(new Error(`Unable to retrieve events0.\n${error.message}`));
}
);
})
.catch((error)=> {
reject(new Error(`Unable to retrieve events 1.\n${error.message}`));
}
);
});
},
// @overwrite discard
discard: (error, action, retries) => {
console.log(error);
//return true;
return retries > 3;
},
// @overwrite retry
retry: (action) => (action.meta.urgent ? 100 : 10000),
//returnPromises: false,
/*
queue: (action) => {
console.log(action);
}
*/
// @overwrite persist
};
export const store = createStore(rootReducer, undefined, offline(offlineConfig2));
一个动作本身。
export const sendRepairPhotos = (identifier, token, auto_number, kilometers, message, photoPath, photoArray = []) => {
console.log('send repair photos');
return ({
type: REPAIR_PHOTOS,
//payload: { token },
meta: {
offline: {
effect: { url: global.apiUrl + 'trucks_malfunctions/register', method: 'POST', body: {km: kilometers,
auto_id: auto_number,
identifier: identifier,
message: message
},
photo: photoArray,
token:token },
}
}
})
};
如果有帮助,我可以附加其他信息,但基本上面临缓存大小问题,以及 react-native 缓存是如何工作的?它会影响内存使用还是只是闲置?我怎样才能设法自动删除它们并进行优化?欢迎大家发表评论。
回答我自己的问题,后来想了想,但也许对其他人也有帮助。我使用的是 RNCAMERA,它将捕获的图像保存在临时文件夹中。
除非应用程序数据和缓存增长,否则通过使用 redux offline 发送带有图像的请求对于删除图像很重要。文件管理模块可以做什么,例如 RN fetch blob、rn fs 等等。
刚刚卡在配置 react-native 离线请求中。我想用它来发送数据和图像文件作为 base64 数组,好吧,我让它工作了,但在那之后缓存不断增长,每天 140mb/4h,同时在真实设备中进行测试,这假设不是它应该工作的方式。我一般缺少什么?
使用自定义请求存储配置(将图像转换为 base64)
offlineConfig2 = {
...offlineConfig,
persistCallback: () => {
console.log('Rehydratation complete');
},
persistOptions: {
key: 'primary',
storage:AsyncStorage
},
rehydrate: true,
// @overwrite effect
effect: (effect, action) => {
console.log(`Executing effect for ${action.type}:`, effect);
photoArr = [];
if (effect.photo)
photoArr = effect.photo;
promises = photoArr.map(function(photo){
return base64 = RNFetchBlob.fs.readFile(photo.key, 'base64')
.then((data) => {
return data
}).catch(err => console.log(err));
});
return new Promise(
function (resolve, reject) {
Promise.all(promises).then(function(base64Photo) {
body = effect.body;
if(Array.isArray(base64Photo) && base64Photo.length > 0) {
body = Object.assign({photoArr: base64Photo}, body);
}
fetch(effect.url, { // eslint-disable-line
method: effect.method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer' + effect.token
},
body: JSON.stringify(body),
})
.then(res => {
console.log('response', res);
resolve('ok');
return res.ok
? res.json()
: Promise.reject(res.text().then(msg => new Error(msg)));
})
.catch((error) => {
reject(new Error(`Unable to retrieve events0.\n${error.message}`));
}
);
})
.catch((error)=> {
reject(new Error(`Unable to retrieve events 1.\n${error.message}`));
}
);
});
},
// @overwrite discard
discard: (error, action, retries) => {
console.log(error);
//return true;
return retries > 3;
},
// @overwrite retry
retry: (action) => (action.meta.urgent ? 100 : 10000),
//returnPromises: false,
/*
queue: (action) => {
console.log(action);
}
*/
// @overwrite persist
};
export const store = createStore(rootReducer, undefined, offline(offlineConfig2));
一个动作本身。
export const sendRepairPhotos = (identifier, token, auto_number, kilometers, message, photoPath, photoArray = []) => {
console.log('send repair photos');
return ({
type: REPAIR_PHOTOS,
//payload: { token },
meta: {
offline: {
effect: { url: global.apiUrl + 'trucks_malfunctions/register', method: 'POST', body: {km: kilometers,
auto_id: auto_number,
identifier: identifier,
message: message
},
photo: photoArray,
token:token },
}
}
})
};
如果有帮助,我可以附加其他信息,但基本上面临缓存大小问题,以及 react-native 缓存是如何工作的?它会影响内存使用还是只是闲置?我怎样才能设法自动删除它们并进行优化?欢迎大家发表评论。
回答我自己的问题,后来想了想,但也许对其他人也有帮助。我使用的是 RNCAMERA,它将捕获的图像保存在临时文件夹中。
除非应用程序数据和缓存增长,否则通过使用 redux offline 发送带有图像的请求对于删除图像很重要。文件管理模块可以做什么,例如 RN fetch blob、rn fs 等等。