Firebase如何自动防止重复条目 [英] Firebase how to prevent duplicate entries atomically

查看:142
本文介绍了Firebase如何自动防止重复条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用Firebase作为Web应用的用户数据的数据存储。我目前的想法是存储每个用户的数据,使用他们加入时的时间戳作为引用该用户数据的关键字。这个方案的优点是它是一个简单的方法来分配唯一的整数ID给用户,并使用户的时序排序简单。



但是,一个不利的方面是,如果两个添加用户请求提交相同的数据,应用程序将愉快地添加两个单独的条目,这是不合理的。我可以洗牌(我开始认为我应该使用电子邮件作为密钥,并通过加入数据进行推荐,而不是我目前的方案),但是假设我不想。有没有办法来防止重复的数据?

天真的做法可能只是做一些事情:



<$如果(!searchFirebaseForUser(data)){
addUser(data); pre>
}

但是这绝对是一个竞赛条件;两个请求都很容易在数据库中查询和找不到任何用户,并且都会添加。我想在交易中这样做,但Firebase交易支持似乎并不涵盖这种情况。有没有办法处理这个问题? 解决方案

您可能必须使用用户名或电子邮件地址作为关键,以原子方式写入该位置。



以下是来自交易功能参考。在这种情况下,我们使用 wilma 作为用户的键。 $ b

  //尝试为wilma创建一个用户,但只有当用户标识wilma还没有被使用时。 
var wilmaRef = new Firebase('https://SampleChat.firebaseIO-demo.com/users/wilma');
wilmaRef.transaction(function(currentData){
if(currentData === null){
return {name:{first:'Wilma',last:'Flintstone'}};
} else {
console.log('User wilma already exists。');
return; // Abort the transaction。
}
},function(error, (错误)
console.log('Transaction failed abnormally!',error);
else if(!committed)
console.log(' ');
else
console.log('User wilma added!');
console.log('Wilma's'data :',snapshot.val());
});


I'm looking at using firebase as a data store for user data for a web app. My current thought is to store each user's data using the timestamp of when they joined as the key referencing that user's data. The advantage of this scheme being that it's a simple way to assign unique integer ids to users, and makes chronological sorting of users simple.

A downside, however, is that if two "add user" requests are submitted with identical data, the app will happily add two separate entries, which is unideal. I could shuffle things around (I'm starting to think I should use email as the key and proritize by join data, rather than my current scheme), but suppose I don't want to. Is there a way to prevent duplicate data?

The naive approach would probably be just to do something like:

if(!searchFirebaseForUser(data)) {
    addUser(data);
}

But this is definitely a race condition; it'd be easy for two requests to both query and find no user in the database, and both add. I'd like to do this in a transaction, but it doesn't seem like the Firebase transaction support covers this case. Is there a way to handle this?

解决方案

You will probably have to use the username or email address as a key, and try to atomically write to that location.

Here is the relevant code sample from the transaction function reference. In this case we use wilma as the key for the user.

// Try to create a user for wilma, but only if the user id 'wilma' isn't already taken.
var wilmaRef = new Firebase('https://SampleChat.firebaseIO-demo.com/users/wilma');
wilmaRef.transaction(function(currentData) {
  if (currentData === null) {
    return {name: {first: 'Wilma', last: 'Flintstone'} };
  } else {
    console.log('User wilma already exists.');
    return; // Abort the transaction.
  }
}, function(error, committed, snapshot) {
  if (error)
    console.log('Transaction failed abnormally!', error);
  else if (!committed)
    console.log('We aborted the transaction (because wilma already exists).');
  else
    console.log('User wilma added!');
  console.log('Wilma\'s data: ', snapshot.val());
});

这篇关于Firebase如何自动防止重复条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆