1

I am creating a mobile app that displays a score of an ongoing soccer game (amateur game, not professional). So, what I am basically doing is, I am creating a data of a soccer game in the Firebase real-time DB which might contain information like:

Team A: String
Team B: String
Score A: int
Score B: int
isLive: true/false
id: int
currentTime: ???

So, when I press the Score Update in my app, it will update the score, and will apply this change to a ListView in my app.

Everything seems clear up to this point, but I am having a trouble of displaying the current game time.

I wanted to work something like:

enter image description here

This is from the web app (http://livescore.com) that I was motivated by. When a game begins, the game time is displayed from 1min to 90min as shown in the image and turns in to FT when it is over.

My question is,

is it possible to make like an automatically incrementing timer in Firebase database, or is it possible to make it work like it is incrementing 1 to its value every 1 minute up to a certain point?

Any kind of advice would be appreciated.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • If you want something to happen periodically in your app, you'll have to provide your own scheduling mechanism. Realtime Database doesn't provide this. – Doug Stevenson Nov 12 '17 at 18:10
  • You can use Google cloud Task Scheduler to trigger firebase functions periodically – Waleed93 Mar 20 '20 at 10:47

3 Answers3

2

There is no way to achieve what you want using Firebase Realtime Database because it doesn't provide something like this. To solve this, you need to create a custom scheduling mechanism made by yourself that provides you that behaviour.

So unfortunately, in this case you do not get rid of coding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Will it be possible to utilize the firebase db though? Or will I have to automatically refresh the app every 1 min? – Dawn17 Nov 13 '17 at 16:05
  • Yes it is possible and you don't have to automatically refresh the app every 1 min. Because Firebase is Realtime database, you need to attach a listener and will be triggered only when something happens, (let's say in you case, the score has changed) – Alex Mamo Nov 13 '17 at 16:09
  • Yeah but I have to display the current time of the match. – Dawn17 Nov 13 '17 at 16:16
  • Yes and that should not be a problem. – Alex Mamo Nov 13 '17 at 16:17
  • So how am I supposed to update the time every 1 min in the real-time database? – Dawn17 Nov 13 '17 at 16:18
  • Yes, to achieve this you only need to attach a listener, get the value of the coresponding minute and after other 60 seconds, increase the time by 1 minute. That's it. – Alex Mamo Nov 13 '17 at 16:22
  • Yes. If you want more than that, please see this [post](https://stackoverflow.com/questions/42790735/cloud-functions-for-firebase-trigger-on-time). So you'll be able to create a function that will add one minute every one minute. – Alex Mamo Nov 13 '17 at 16:28
  • Hi @Dawn17 did you ever implement the timer for your app? – flutter Jun 06 '18 at 13:00
  • @Dawn17 be grateful if you could share what you did? – flutter Jun 07 '18 at 09:05
0
//fetch test time in minutes
    //and convert that into milli seconds
    mTimerRef = FirebaseDatabase.getInstance().getReference("eTest").child(key).child("TestMinutes");
    mTimerRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            long millisecs = (Integer.parseInt(dataSnapshot.getValue(String.class))) * 6000;
            mTimeLeftInMillis = millisecs;
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

add minutes in firebase and read that using this function and convert into long, milli seconds. hope this help.

0

Most likely, you need another client that will always work and update the global time in FireBase, and based on the global time, you can make your calculations