programing

파이어베이스 업데이트 대 세트

itmemos 2023. 7. 11. 21:35
반응형

파이어베이스 업데이트 대 세트

제목에서 알 수 있듯이, 저는 그 차이를 이해할 수 없습니다.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똑같은 일을 합니다.

잘 부탁드립니다, 베네

원자성

당신이 준 두 샘플의 큰 차이점은 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" });

기존 게시물 전체를 대체하게 됩니다.그래서 원본.uid,author그리고.starCount들판은 사라지고 새로운 것이 있을 것입니다.title그리고.body.

반면에 업데이트를 사용하는 경우:

firebase.database().ref().child('/posts/' + newPostKey)
        .update({ title: "New title", body: "This is the new body" });

이 코드를 실행한 후 원본uid,author그리고.starCount업데이트된 내용뿐만 아니라 여전히 존재할 것입니다.title그리고.body.

언급URL : https://stackoverflow.com/questions/38923644/firebase-update-vs-set

반응형