2

Well, this is my first Android app using Firebase and I'm a little bit worried about the storage of the database.

Here comes my problem; I'm working on an app which shares users posts, but I want users to see only posts of their own country or city. As Firebase does take in account the use of the database and the storage too, if the user must travel around all the posts of the world just to find the ones which were shared in his city, he will probably use to much data.

So how can I structure my database in order to let the user just get posts published around him without filtering all the posts around the world? Is there a way to achieve this? Can somebody share his experience if he has already encountered this problem?

Thanks in advance !

Roman Dev
  • 103
  • 2
  • 11

2 Answers2

2

When a user posts a post, one of the key-value pairs could be "location" : "New York, NY". If the user who is viewing posts is in "New York, NY", then the app can query the database for posts in "New York, NY". I am assuming you know or can figure out how to get location data from the user and generalize it so that the post shows up under a more generalized "New York, NY", instead of something more specific, such as "Long Island, NY" (unless you want it to be more specific. In Swift, the code could be something like:

FIRDatabase.database().reference().child("posts").queryOrdered(byChild: "location").queryEqual(toValue: "New York, NY")

It should be the same idea in Java (Android)

In this case, the database will return all posts in the area, "New York, NY"

Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • I'm not convinced about this idea because it is easy to use in USA for example, the states are clearly defined. But imagine somewhere else, it can be a little bit difficult. I can repeat my question in a different way: knowing location of the posts and knowing the current location of the user, is there a way to filter posts without downloading them to determine the distance to them? – Roman Dev Dec 10 '16 at 08:46
1

So your problem is more like a publish-subscribe messaging system is needed here like Kafka.

If I have understood your problem right, you want to have feeds of nearest cities/areas after fetching the current location of your application user. Right now, we've many tools and technologies to serve this purpose (e.g. Kafka). But this problem is Firebase specific, so that no server side coding is required. I'm not sure about giving a solution to your problem, but lets just think how the problem can be solved and then we can try to replicate the solution in a Firebase way.

We may think of a channel where the posts from each user of your application is dumped. Each post has a key identifying their location. Now let us think of each user of your application as a subscriber with some specific topic in that channel. As soon as any subscriber (i.e. an user of your application) comes, the channels feeds that subscriber with the posts they want (i.e. the posts of nearby cities).

For example, lets we have some posts in that channel having keys, NY, Dhaka, Khulna, NJ, London, Seoul, Sydney etc. Now someone from Jessore just have subscribed in your channel and the nearby cities for him is Dhaka and Khulna. So the subscriber has the key Dhaka, Khulna and when the channel sees they key, it serves the posts containing location with the key Dhaka and Khulna.

Now how can we achieve this behaviour in Firebase? That's the challenge we're facing right at this point. So let us have a channel node having all posts from all location (like the channel described earlier). Here's an example data structure.

channel
  - post1
    - content : "this city is beautiful"
    - location: "Khulna"
  - post2
    - content : "This city is okay"
    - location: "Dhaka"
  - post3
    - content : "London, here I come"
    - location: "London"
  - post4
    - content : "leaving the city"
    - location: "NY"

Now, you need to have a distance-mapping among the cities, which will tell you the nearest cities when you pass your current city to that table. You need to find out your own distance mapping for the cities given. Lets say, your current city is Jessore. Your distance mapping table should return the nearest cities (e.g. Dhaka and Khulna). This might be a complex one to build. But once the relation mapping is built, its merely will be needed to update. For example, the final state of your distance-maping data will look like.

distance-mapping
  - Dhaka
    - Khulna
    - Jessore
  - Khulna
    - Dhaka
    - Jessore
  - Jessore
    - Dhaka
    - Khulna
  - Vegas 
    - NY
  - NY
    - Boston
    - Vegas

Now when you get the current location of your user, you'll easily find the nearest cities from the distance-mapping table in Firebase. Pass the cities to the channel table get the posts having those specific keys.

Here's some answers regarding complex queries in Firebase. You might get help passing select query parameter from these.

Hope that helps!

Community
  • 1
  • 1
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • I think you're not completely asking my question but I feel you have understood my problem and you're giving me some valious hints. Can I take your mail for talking about it? Besides, the answer fits with the question and I'll think I'll check it as the correct one. – Roman Dev Dec 10 '16 at 15:56