0

I'm using both Google Firebase and Room Database to store Event objects I have created, containing all the details of a given event. I use Firebase for online storage and user interaction, and I use Room Database for an easier, persistent RecyclerView implementation.

My problem is that my function for populating the Room Database with events in the user's radius doesn't seem to execute at all in NEITHER an asynchronous task NOR a Room Database callback that overrides the onCreate method. The function was fully debugged while it was used on the main thread, so I think the reason why it isn't working right now has to do with my lack of understanding of how asynchronous tasks work.

Here is my function, currently within the onCreate method of the Room Database class:

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference().child("events").child("eventid");

    if (ref != null) ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Event event = dataSnapshot.getValue(Event.class);
                if (event != null) {
                    LatLng user_location = new LatLng(current_user.get(0).getLatitude(),
                            current_user.get(0).getLongitude());
                    LatLng event_location = new LatLng(event.getLatitude(), event.getLongitude());
                    int distance_preference = current_user.get(0).getDistance();
                    double distance_between_user_and_event = SphericalUtil
                            .computeDistanceBetween(user_location, event_location) / 1609.344;
                    if (distance_between_user_and_event <= distance_preference) {
                        eventDao.insert(event);
                    }
                }
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

In an asynchronous task, I can't really debug it since it isn't on the main thread (unless I don't know how lol), and this code doesn't seem to execute at all when in the onCreate method for Room Database. There's obviously something I'm missing here.

Also, it's worth saying that my EventDao, EventDatabase, and EventRepository have all been fully debugged. I can use all of them perfectly fine at runtime -- it's just this early population task that isn't working!

Thanks so much for any help!!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Nov 04 '18 at 07:32
  • Thanks so much for the comment, but I don't believe this will help me solve the problem. My functions aren't returning anything, so asynchronicity isn't an issue. I just add each UserData instance to the Room Database when it becomes available, which should work based on what that thread was saying. My problem is that Room Database will throw an exception and crash the app if you try and do ANYTHING on the main UI thread. So then I moved all these operations to an asynchtask, and they stopped working, even though they were working before. But maybe I don't understand the article. – Braedan Shigley Nov 04 '18 at 17:17

0 Answers0