[Firebase] onsnapshot, get μ°¨μ΄ / νΈμ μμ μ€μκ° λ°μνκΈ°
1. λ¬Έμ μν©
νΈμ μμ λ²νΌ ν΄λ¦ μ νμ΄μ΄λ² μ΄μ€ DBμμλ μμ λμμΌλ νλ©΄μ λ°λ‘ λ°μλμ§ μμ
μλ‘κ³ μΉ¨νκ±°λ λ€λ₯Έ νμ΄μ§μ κ°λ€κ° λ€μ λ€μ΄μμΌ νλ©΄μ λ°μλ¨
2. λ€λ₯Έ νλ©΄μμ νΈμμ μμ ν λλ λ°λ‘ λ°μλ¨
Profile νμ΄μ§μμ νΈμμ μμ ν λλ νλ©΄μ λ°λ‘ λ°μλμ§ μμμ§λ§,
Home νμ΄μ§μμλ μμ λ λ΄μμ΄ νλ©΄μ λ°λ‘ λ°μλ¨
3. μ½λ λΉκ΅ (Profile vs Home)
νλ©΄μ λ°λ‘ λ°μλμ§ μμλ Profile μ»΄ν¬λνΈμμλ get ν¨μλ₯Ό μ¬μ©νκ³ ,
νλ©΄μ μ¦κ° λ°μλμλ Home μ»΄ν¬λνΈμμλ onSnapshot ν¨μλ₯Ό μ¬μ©νμ
Homeμμλ νΈμ μμ μ 리λ λλ§μ΄ μΌμ΄λ¬κ³ , Profileμμλ 리λ λλ§ λμ§ μμ (console.logλ‘ νμΈ)
// Profile.jsx (getν¨μ μ¬μ©, 리λ λλ§X)
const getMyTweets = async () => {
await dbService
.collection("tweets")
.where("creatorId", "==", userObj.uid)
.orderBy("createdAt", "desc")
.get()
.then((data) => {
const tweetArray = data.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
setMyTweets(tweetArray);
});
};
useEffect(() => {
getMyTweets();
}, []);
// Home.jsx (onSnapshotν¨μ μ¬μ©, 리λ λλ§O)
useEffect(() => {
dbService
.collection("tweets")
.orderBy("createdAt", "desc")
.onSnapshot((snapshot) => {
const tweetArray = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
setTweets(tweetArray);
});
}, []);
4. get() vs onSnapshot()
- Firestoreμμ λ°μ΄ν°λ₯Ό λΆλ¬μ€λ λ°©λ²μ get(), onSnapshot() λκ°μ§κ° μμ
- get()μ λ°μ΄ν°λ₯Ό λ± 1λ²λ§ λΆλ¬μ΄. μ€μκ° λ°μ μλκ³ , λ³κ²½λ λ΄μ©μ νμΈνλ €λ©΄ get()μ λ€μ νΈμΆν΄μΌν¨
- onSnapshot()μ λ°μ΄ν° λ³κ²½μ κ°μ§νλ 리μ€λλ₯Ό λ¬μμ£Όλ κ²μ. λ°μ΄ν°κ° λ³κ²½λ λλ§λ€ μ€λ μ·μ΄ μμ±λμ΄, μ€μκ°μΌλ‘ λ³κ²½λ΄μμ νμΈν μ μμ
- λ°μ΄ν°λ₯Ό λΏλ €μ£ΌκΈ°λ§ ν λλ get(), CRUDλ₯Ό μ€μκ°μΌλ‘ λ°μν΄μΌ ν λλ onSnapshot() μ¬μ©
There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:
- Call a method to get the data.
- Set a listener to receive data-change events.
When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes.
When you use get() you "retrieve the content of a single document" only once. It's a kind of "get and forget": If the document changes in the (back-end) Firestore database you will need to call get() again to see the change.
On the opposite, if you use the onSnapshot() method you constantly listen to a document as explained in the doc:
You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.
As explained in these docs, these two methods apply to one document or to a collection of documents (including a query).
5. μ½λ μμ get()->onSnapshot()
onSnapshotμΌλ‘ λ°κΎΈλ Profile νμ΄μ§μμλ νΈμ μμ μ λͺ©λ‘μ λ°λ‘ λ°μλ¨
// Profile.jsx
useEffect(() => {
dbService
.collection("tweets")
.where("creatorId", "==", userObj.uid)
.orderBy("createdAt", "desc")
.onSnapshot((snapshot) => {
const tweetArray = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
setMyTweets(tweetArray);
});
}, []);