FileSystem 은 보통 디렉토리를 생성하는 과정과 읽기/쓰기 과정으로 분리된다.
redux와 같이 사용할 경우, saga에서 읽기 부분을 처리하는것이 일반적인데, 그 때 FileSystem 라이브러리는 아래와 같이 사용할 수 있다.
# 오류가 있을 수 있는 코드입니다. 주의하세요
import { call, put, takeLatest, select } from "redux-saga/effects";
import { actions } from "@store/Room";
import * as FileSystem from "expo-file-system";
import { ensureChatDirExists, getChatFileUri } from "@components/File/ensure";
function* openRooms(action) {
const { updateRoomChatData } = actions;
try {
yield call(ensureChatDirExists);
const stringData = yield call(FileSystem.readAsStringAsync,
getChatFileUri(action.payload)
);
const data = JSON.parse(stringData);
yield put(updateRoomChatData(data));
} catch (err) {
//TODO sentry
console.log("FAIL Read Chat File!!!", err);
}
}
export function* watchOpeningRoom() {
const { openRoom } = actions;
yield takeLatest(openRoom, openRooms);
}
참고: https://gist.github.com/jdrouet/29db4a5f337e88d84f8d10bfd2b17673