Sequential Number

527 views
Skip to first unread message

MO CISSE

unread,
May 11, 2018, 2:04:03 PM5/11/18
to Firebase Google Group
Hi Everyone, 
How can I generate a sequential number with Firebase ? 
I am trying to create a transaction application. 
Let say my app has 2 users : 
Cisse (id=01) 
Dawson(id=02). 
If Cisse create 1st transaction for today(04/27/18) , the transaction number will be 01042718001 (01=userid, 042718=today date,001 transaction#). 
If Cisse create another transaction again today , the number will be : 01042718002 
The first transaction for Dawson today will be : 02042718001 
The first transaction for Dawson tomorrow will be : 02042818001 

How can I do that ?
Thanks

Andreas B

unread,
May 11, 2018, 6:04:57 PM5/11/18
to Firebase Google Group
To generate numbers according to your rules, a client would just need to know three things:
1. the ID of a user
2. the current date
3. the number of transactions that have already taken place for this user, today

Regarding (1), Firebase Auth can be used to get a unique ID for each of your users: https://firebase.google.com/docs/auth/

(2) is trivially done using whatever Date/Time functions your platform has to offer.

For (3), one way to do this might be to store, for each of your users, the date and number of the last transaction. If another transaction is supposed to happen, either increase number (if date is today), or reset number to 1 and update date to today's date (if date is earlier than today). Storing and retrieving user data can for example be achieved using Firebase Realtime Database: https://firebase.google.com/docs/database/

Using your numbering scheme, you might run into different problems, though: What if a user creates more than 999 transactions on a single day? Do numbers need to be sequential across days? - because using MMDDYY, they are not. Should transactions be sequential across users? - because in that case, it might be more sensible to have the ID at the end of the string instead of the beginning. What if two transactions of the same user are started concurrently (for example on two different devices), leading to a race condition where both might end up with the same number?

I'm not sure what your goal really is with these numbers. If you are trying to create something like unique IDs for an arbitrary number of transactions to use as keys in a database, something like push() might be what you're really looking for: https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference.html#push()

CISSE M.Y.

unread,
May 12, 2018, 12:40:23 AM5/12/18
to fireba...@googlegroups.com
Hi Andreas,
Do you have a sample code ?
Thanks for your help.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/6976f8a8-8b59-4b5d-a17e-666cee8469da%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
CISSE M. Y.
Sofware/Web Developer
Phone :(347) 443-9028


Andreas B

unread,
May 12, 2018, 6:17:09 AM5/12/18
to Firebase Google Group
I don't, but here's a blog post about the unique ID values generated by push(), in case you want to read up on that: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

There are additional code examples on the various pages I already linked to.

CISSE M.Y.

unread,
May 12, 2018, 10:51:52 AM5/12/18
to fireba...@googlegroups.com
Hi,
I needed only digits not alpha numerical.
Let say i created an invoice or package tracking number.
It is difficult to tell a customer , your invoice or tracking number is : JhLeOlGIEjaIOFHR0xd
Thanks


On Sat, May 12, 2018 at 6:17 AM, Andreas B <and...@bocops.de> wrote:
I don't, but here's a blog post about the unique ID values generated by push(), in case you want to read up on that: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

There are additional code examples on the various pages I already linked to.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andreas B

unread,
May 14, 2018, 5:10:52 AM5/14/18
to Firebase Google Group
For any real application, your "number string" might still become very large, though. For example, if you plan to eventually have more than 100 users (your two-digit ID would only allow that many), the ID would need to be longer. Same if you want to allow more than 1,000 transactions to happen on a single day. Last but not least, it might be important for this number to not be sequential and guessable - in which case, having all information (ID, date, #) available as clear text with no additional "filler" is not a good idea.

Even assuming that all of this is not relevant, some of the other parts like making sure that the next available transaction# is properly stored and updated are still non-trivial. It would be better if you could show what you already have, and especially what Firebase products you are using (I assumed RTDB before, but that might not even be the case).

CISSE M.Y.

unread,
May 14, 2018, 10:45:57 AM5/14/18
to fireba...@googlegroups.com
Thanks

On Mon, May 14, 2018 at 5:10 AM, Andreas B <and...@bocops.de> wrote:
For any real application, your "number string" might still become very large, though. For example, if you plan to eventually have more than 100 users (your two-digit ID would only allow that many), the ID would need to be longer. Same if you want to allow more than 1,000 transactions to happen on a single day. Last but not least, it might be important for this number to not be sequential and guessable - in which case, having all information (ID, date, #) available as clear text with no additional "filler" is not a good idea.

Even assuming that all of this is not relevant, some of the other parts like making sure that the next available transaction# is properly stored and updated are still non-trivial. It would be better if you could show what you already have, and especially what Firebase products you are using (I assumed RTDB before, but that might not even be the case).

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

shyaka james

unread,
May 14, 2018, 10:46:40 AM5/14/18
to fireba...@googlegroups.com
the problem it is not number of movement the problem is once i ploaded apk on playstore every thing not working 


On Mon, May 14, 2018 at 11:10 AM, Andreas B <and...@bocops.de> wrote:
For any real application, your "number string" might still become very large, though. For example, if you plan to eventually have more than 100 users (your two-digit ID would only allow that many), the ID would need to be longer. Same if you want to allow more than 1,000 transactions to happen on a single day. Last but not least, it might be important for this number to not be sequential and guessable - in which case, having all information (ID, date, #) available as clear text with no additional "filler" is not a good idea.

Even assuming that all of this is not relevant, some of the other parts like making sure that the next available transaction# is properly stored and updated are still non-trivial. It would be better if you could show what you already have, and especially what Firebase products you are using (I assumed RTDB before, but that might not even be the case).

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages