I want to store a long value as string for various reasons so im trying to convert serverValue.TIMESTAMP to string but not able to. Can someone helpe me out?
Code I tried
mDatabaseReference.child("Chats").child(MessageSenderId).child(MessageRecieverId)
.child("Seen").setValue(String.valueOf(ServerValue.TIMESTAMP));
Result
"{.sv=timestamp}"
The database is storing the value like that and i have no idea why. I just can't store it as a long value, so I need to convert it to string because while recieving all the values should be String due to some feature I have. So can someone help me in converting this long to string value?
The ServerValue.TIMESTAMP is a so-called marker that you send to the database server. The database server then expands that into the actual timestamp. As you can see in your string version, the marker is actually a map.
There is no way to instruct the database to store the ServerValue.TIMESTAMP value as a string. Your options are:
Modify the feature to deal with the actual numeric value.
Store the client-side timestamp as a string from your app, with setValue(String.valueOf(System.currentTimeMillis()))
Write a Cloud Function that triggers on the server-side timestamp and then converts it to a string.
Related
I want to add a timestamp of a data entry creation, example:
myRef.child("uid").setValue(data);
//add timestamp to the same path myRef.child("uid").setValue(<timestamp here>)
What would be the best timestamp, so that it will be independent of any time zone? (For example if a user's phone clock is set to the wrong time)
I did see this article from Firebae docs: Timestamp, but can't figure how to use it.
Use the value placeholder firebase.database.ServerValue.TIMESTAMP to update a value with the current clock time on the server at the time of the write, as seen by the server. It's a token value that means nothing on the client but has a special meaning on the server when it's written.
inside Firebase Functions transform the timestamp like so:
timestampObj.toDate()
timestampObj.toMillis().toString()
documentation here https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp
i am trying to use compareTo method based on firebase docs to get how long ago was the database entry created.
https://firebase.google.com/docs/reference/android/com/google/firebase/Timestamp
I am trying to do something like this
var postedAgo = myEntry.dateCreated.compareTo(firebase.database.ServerValue.TIMESTAMP);
myEntry.dateCreated is stored TIMESTAMP, so all i got in my db is numerical value. I hope thats right. But the problem is when i log this it says that compareTo is not a function.
I am obviously doing something wrong, but i cant find almost anything on compareTo beyond the documentation. I wonder if anyone is even using it.
Thanks
Luke
The Firebase Database ServerValue.TIMESTAMP is not an actual value of a timestamp, but a so-called marker value that the server recognized (and then replaces by the current timestamp when writing to the database). Because of this, you cannot use ServerValue.TIMESTAMP in pure client-side operations.
If you want to determine how long ago a node was created, you'd take the timestamp from that node and subtract it from the current timestamp. So if myEntry.dateCreated is a timestamp that was written with ServerValue.TIMESTAMP, you could do:
System.currentTimeMillis() - myEntry.dateCreated
I am tryign to store the current username in the class and having such a tough time.
This is only part of the code
String usernameNewbet = currentUser.getUsername().toString();
bets.put("EndDate", endDate);
bets.put("EndTime", actualTimeString);
bets.put("Player_Pointer", usernameNewbet);
But when I am trying to save I am getting this error.
You probably created a Pointer column named "Player_Pointer" and tried inserting a String into it. When saving pointers in Parse, you're supposed to provide the actual object, in this case the User itself, to the field, and not the objectId.
In your case, change this:
String usernameNewbet = currentUser.getUsername().toString();
bets.put("Player_Pointer", usernameNewbet);
To this:
bets.put("Player_Pointer", currentUser);
I am getting a kmz-file from a webservice, which I use for geofencing.
The app is responsible to check whether the gps-location of the phone is within the geofence, or not.
I don't really know how the kmz-file will be structured (I am not creating it), but I think the coordinates might look like this:
<coordinates>
-112.2550785337791,36.07954952145647,2357
-112.2549277039738,36.08117083492122,2357
-112.2552505069063,36.08260761307279,2357
-112.2564540158376,36.08395660588506,2357
-112.2644963846444,36.08627897945274,2357
-112.2656969554589,36.08649599090644,2357
</coordinates>
How can I check if the gps-coordinates of my phone are within the geofence (the example above is only a line, it should be a closed area, for example a rectangle)? Right now I can't really think of how to do that.
And what library should be used to access the kmz-files?
First, for geofencing, download the sample from here and read the documentation. And I don;t know about kmz file. But if you can get the string from it(using file reading) and know its structure, then you can easily parse it. If the format is like you mentioned, then you can get the values by following method:
Split the string on coordinates (including <>). You will get a string array. Take the string at index 1.
Split this string on /coordinates (including <>). You will get another string array. Take the string at index 0.
Now split this string on ",". You will get an array of strings again! Now at indexes 0,3,6... are latitudes and at indexes 1,4,7... are longitudes and at indexes 2,5,8... are the third number in the data you mentioned.
can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.