Android "Singleton" Pattern?

676 views
Skip to first unread message

Doug Thompson

unread,
Jun 26, 2015, 7:57:06 AM6/26/15
to fireba...@googlegroups.com
Am wondering if someone can point us in the direction of a code sample or other thoughts on how to create the equivalent of an iOS Shared Instance for a Firebase listener on Android?

Most of the example code embeds the Firebase addValueEventListener and other calls inside an Activity. But I'd be interested in seeing how to create an addValueEventListener so that changes to the Firebase snapshot are accessible across multiple activities.

Are there examples of creating a shared Firebase class so that multiple Activity (app pages) can receive arrays or updates from that shared snapshot?

Sorry for a newcomer question - am trying to wrap my head around this to create consistency between Android/iOS approaches.

Kato Richardson

unread,
Jun 26, 2015, 11:53:57 AM6/26/15
to fireba...@googlegroups.com
Doug,

Could you explain the goal here? It's hard to understand why a singleton would be useful, since Firebase already optimizes and shares the data cache internally. The snapshots are immutable, and adding multiple listeners should be trivial for cost and perf, so I can't really see why sharing a listener would be of any use, or how this would work exactly.

☃, Kato

--
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-tal...@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/c36d4836-872b-4d25-a994-29d04b780015%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evans Attafuah

unread,
Jun 27, 2015, 4:54:53 AM6/27/15
to fireba...@googlegroups.com
I solved this problem using a global listener. Each user in my app has a global listener where other users can write snips of data updates to. like "lastMessage sent when user is inactive". Since firebase is web socket having multiple listeners to the same reference is not a problem. Plus I try to solve the solution with a singleton but wasn't helpful.

1. just have a <global/userId> listener in the parent/root activity on create 
2. or have the global listeners in each activities onResume and destroy in onPause. that also works. 
3. have a <global/userId> listener in the parent/root activity on create. and have the same listener in other activity on create.

What I find convenient is using the builder pattern so the code to add in each activity is not overwhelming. 

for example 
//child added, child change etc
//I have attached a screenshot of my global listener view
globalChannel new GlobalChannel.Builder()
        .init()
        .status(deviceUserId)
        .addStatusListener(this)
        .load()
        .connect();

contextUserChannel new UserChannel.Builder()
        base reference - https://your-firebase-url
        .init()
         firebase reference - <userId/ConversationId>
        .channel(contextUserIdconversationId)
         firebase reference - active:true/false
        .active(false)
         keepSync feature
        .sync()
         value change listener
        .addActiveListener()
         firebase reference - type:true/false
        .typingIndicator()
         value change listener
        .addTypingIndicatorListener(this)
         start listening
        .connect();

messageChannel new MessageChannel.Builder()
        .init(conversationId)
        .chatBetween(deviceUserIdcontextUserId)
        .addMessageListener(this)
        .loadAllPreviousMessages(this)
        .connect();

//remove event listeners conveniently 
messageChannel.disconnect();
contextUserChannel.disconnect();
globalChannel.disconnect();

makes it easier to develop and manage. hope this helps
Screen Shot 2015-06-27 at 8.50.52 AM.png

Doug Thompson

unread,
Jun 27, 2015, 8:52:13 AM6/27/15
to fireba...@googlegroups.com
Evans - 

Thank you SO much. This is perfect, just what I was after!

Appreciate your generosity of time and insight.
Reply all
Reply to author
Forward
0 new messages