Asyl Paitesh
Asyl Paitesh

Reputation: 161

How to avoid inconsistency when adding data to Firestore?

We are building an app where we want to display cities. Each city has also an image which is stored in Firebase Storage. We are adding data to the db either using the Firebase console or programmatically. The problem arrives when we add data that contain special characters, for instance, I have this url:

https://firebasestorage.googleapis.com ... München.png

This is how it looks like in the browser. If we are adding this url using the Firebase console it will be saved the same as above, however, when we do it programmatically, that url is saved:

https://firebasestorage.googleapis.com ... M%C3%BCnchen.png

So the following query:

db.collection("cities")
    .whereEqualTo(
        "cityPictureUrl",
        "https://firebasestorage.googleapis.com ... München.png"
    );

Won't work since the name in the database is M%C3%BCnchen and not München. How to have the data stored in most correct way to avoid inconsistency?

Upvotes: 0

Views: 69

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

You may encode the URI before querying, as follows:

String imageURI = "https://firebasestorage.googleapis.com ... München.png";
String imageURIEncoded = URLEncoder.encode(imageURI, "utf-8");  

db.collection("cities")
    .whereEqualTo(
        "cityPictureUrl",
        imageURIEncoded
    );

URLEncoder.encode() will "encode a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character"

Upvotes: 1

Related Questions