About saving View objects in firebase database android - android

Im currently working on drawing app and I need to save draw of one of the player in the database
I undrestood that I supposed to use setValue method to save data on specific child:
rooms.child("room " + player.roomNumber).child("draw").setValue(paintView);
"paintView" is my View object.
When running I get this error message:
com.google.firebase.database.DatabaseException: Found conflicting getters for name: isFocusable

The error message is suggesting that you can't use an Android View object as a value in Realtime Database. You can only store data types that are compatible with JSON: string, number, boolean, array, object.
If you're trying to save image data, it's probably not a good idea to use a database at all. Consider instead uploading image data to Cloud Storage instead. You will need to write code to extract the image data from any views you're working with.

Related

Firebase getreference() vrs getreference(value)

My firebase contains different data trees like Users, hobbies, class, scores etc. I want to get values from two sets of data “Users” and “hobbies”.
What is the best way to get values from the two tables?
Should I use firebaseDatabase.getReference() and then
dataSnapshot.child("Users").child(“name”).getValue().toString();
dataSnapshot.child("hobbies ").child(“track”).getValue().toString();
Or do I have to firebaseDatabase.getReference(“Users”) and firebaseDatabase.getReference(“hobbies”);
Since I noticed that firebaseDatabase.getReference() seem to refer to all data including the ones that are not needed(class, scores etc). Will this cause the app to slow down or does it have any implications?
If you attach a listener to a DatabaseReference, it will download/read all data under that reference. So if you attach a listener to FirebaseDatabase.getInstance().getReference(), you are reading all data in your database.
If you only need a subset of all data in your app, it's more efficient to only load that data. This means that you'll need to attach a separate listener to each branch of data that you need.

Parcelable out of memory

Suppose we have a list of complex objects (primitives and other objects inside) requested from a server to show them inside a RecycleView. In adapter we need only some data from each object, let's say 70%.
I get from server list with full data objects, and each object implements Parcelable so when I select a item I pass object via intent to MyDetailsActivity like so:
Intent intent = new Intent(context, MyDetailsActivity.class);
intent.putExtra("foo", myComplexObject);
startActivity(intent);
This is working as expected but on some low memory devices I get out of memory errors. So my question is how to achieve this type of data flow?
One possible solution is to implement get/set for MyObj in Applicattion class, and pass it like so but I'n not sure if it's stable.
And of course I can pass just an id from MyObject and do another request call inside DetailsActivity's onCreate(), but this requires user to wait some more seconds to load data.
Any advices or references are apreciated
As you have experienced, sending data through the bundle/intent extras has its memory limits.
Your options are to either keep those objects in memory and access them via some static/singleton instance or to do the data query from your server in the desired activity that will show them in your list.
Another viable options is to store the data in a database for ex and read it once its required but this ofcourse depends on your current architecture... you can checkout Realm, Room, GreenDao database options etc...
Second approach is more suitable for mobile applications since you only load data in list via API which is visible inside the recycler view, you can pass that data to activity and then in activity load the remaining data from another call for that specific item.
I'm assuming that each object of the data coming from the server is not of same size, leading to out of memory on some but not all the objects. So the better approach is to also modify server apt (if possible) to send only info which is required for the list in your call's response and have separate resource url to access full information for that object.
This may result in a minimal delay but it will not cause unexpected out of memory errors which is a big let down for the end user.
You can actually use Application class for this purpose, to make it even better
Set the data in Application class when you intend to pass
Retrieve the data at destination into Local variable from Application
After retrieving data into local variable, set object in Application to null
Using above approach will make sure your objects memory is released as soon you consumed it without holding it all the time throughout Application Lifecycle

Firebase - Android: Retrieve ArrayList from Firebase - Failed to bounce type

I am facing the following issue:
I upload an object to my firebase and one of the attributes is a ArrayList.
This works like a charm and I get all the objects with their attributes into my firebase.
When I want to retrieve this data I get following error:
com.firebase.client.FirebaseException: Failed to bounce to type
This happens, because I cannot retrieve the data from the Firebase as an ArrayList and import it in an the same type of object I used to upload.
How can I consume these type of firebase objects into a java object?
Thanks a lot
I finally managed to find a solution.
"Groupmember" is an Arraylist, but it does contain HashMap instead of the Object I passed for upload - makes sense! :-)

how can I use Text datatype in app engine entity

I am getting problems in using Text datatype in entity class for storing values more than 500 characters long while using app engine. As com.google.appengine.api.datastore.Text is not available in my android activity class, how do I create a Text datatype in my android class?
I tried using below code while defining my entity class, but I still get illegalargument exception when trying to use get/set methods in my android activity class.
private Text storyContent;
public String getStoryContent() {return storyContent.getValue();}
public void setStoryContent(String storyContentString) {
this.storyContent=new Text(storyContentString);
}
updating App engine SDK to the latest one(1.8) seems to have resolve the issues. Looks like it was a known issue in app engine. I guess for longer than 500 characters long values, datastore is internally converting to Text type. hence I can see my values are being stored at Text type in datastore viewer.
http://code.google.com/p/googleappengine/issues/detail?id=9035

Saving player data and status

I made an android game and I want to know how to save player data such as money, level reached, etc. I think that XML is not a really convenient way to do this. Is there any good way to do this? Does android provide some method to save the data?
A database is probably overkill for the type of data you need to track. I would suggest using Android's SharedPreferences APIs. See here for a tutorial.
Here's an official overview of Android data persistence methods including SharedPreferences and SQLite.
Check out the getStringSet API for storage of arrays/collections.
For non-existent vals for the string set:
Returns the preference values if they
exist, or defValues. Throws
ClassCastException if there is a
preference with this name that is not
a Set.
For non-existent value data such as string, bool, float etc.:
Returns the preference value if it
exists, or defValue. Throws
ClassCastException if there is a
preference with this name that is not
a String.
Use a SQlite database. ( Data storage on developers.android )
Here is a tutorial that shows the functionality:
http://p-xr.com/android-tutorial-simple-but-persistent-data-storage/

Categories

Resources