Firebase 업데이트 대 세트
제목이 말하듯이, 나는 사이의 차이를 얻을 수 update
및 set
. 또한 set을 사용하면 업데이트 예제가 똑같이 작동하므로 문서가 나를 도울 수 없습니다.
update
문서 의 예 :
function writeNewPost(uid, username, title, body) {
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0
};
var newPostKey = firebase.database().ref().child('posts').push().key;
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
사용하는 동일한 예 set
function writeNewPost(uid, username, title, body) {
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0
};
var newPostKey = firebase.database().ref().child('posts').push().key;
firebase.database().ref().child('/posts/' + newPostKey).set(postData);
firebase.database().ref().child('/user-posts/' + uid + '/' + newPostKey).set(postData);
}
지금의 모습 때문에 그래서 아마 문서에서 예, 업데이트해야 update
하고 set
똑같은 일을.
감사합니다, Bene
원 자성
제공 한 두 샘플의 큰 차이점은 Firebase 서버로 보내는 쓰기 작업의 수입니다.
첫 번째 경우에는 하나의 update () 명령을 보냅니다. 전체 명령은 성공하거나 실패합니다. 예를 들어 사용자에게에 게시 할 수 /user-posts/' + uid
있는 권한이 있지만에 게시 할 수있는 권한이없는 경우 /posts
전체 작업이 실패합니다.
두 번째 경우에는 두 개의 개별 명령을 보냅니다. 동일한 권한을 사용하면 쓰기 /user-posts/' + uid
는 성공하고 쓰기 /posts
는 실패합니다.
부분 업데이트 대 전체 덮어 쓰기
이 예에서는 또 다른 차이점이 즉시 보이지 않습니다. 그러나 새 게시물을 작성하는 대신 기존 게시물의 제목과 본문을 업데이트한다고 가정 해보십시오.
이 코드를 사용하는 경우 :
firebase.database().ref().child('/posts/' + newPostKey)
.set({ title: "New title", body: "This is the new body" });
You'd be replacing the entire existing post. So the original uid
, author
and starCount
fields would be gone and there'll just be the new title
and body
.
If on the other hand you use an update:
firebase.database().ref().child('/posts/' + newPostKey)
.update({ title: "New title", body: "This is the new body" });
After executing this code, the original uid
, author
and starCount
will still be there as well as the updated title
and body
.
참고URL : https://stackoverflow.com/questions/38923644/firebase-update-vs-set
'programing tip' 카테고리의 다른 글
cpp 프로젝트 폴더를 통해 clang-format을 호출하는 방법은 무엇입니까? (0) | 2020.12.01 |
---|---|
@SuppressWarnings에 "비공개 가능"경고를 표시하지 않으려면 어떤 값을 사용해야합니까? (0) | 2020.12.01 |
emacs에서 탭 문자 찾기 (0) | 2020.12.01 |
WinForm의 Dispose 메서드를 어떻게 확장합니까? (0) | 2020.12.01 |
파생 클래스가 인스턴스화 될 때 추상 클래스 생성자가 암시 적으로 호출되지 않습니까? (0) | 2020.12.01 |