when I implemented my DB with OrmLite, I was able to let backreferences be created automatically. For example, imagine a class A which has a reference to a list of objects of class B and Class B has a reference back to that specific object of class A. With Gson/OrmLite, when parsing the following JSON it would create a obejct of class A with a ForeignCollection of Class B objects and for every class B object it would create a reference to the class A object (if I am not mistaking anything here). Take the following json (representing an object of Class A):
{
"id":5,
"objectsOfTypeB": [
{
"id":2,
"name":"pete"
},
{
"id":4,
"name":"mathew"
}]
}
Now I would like to do the same with Realm. Is it somehow possible to create a backreference for nested objects? Maybe hook into the object-creation process and set the reference manually?
Christian from Realm here.
The core database support the notion of backlinks, which is what you are looking for, but they havn't been exposed in the Java API yet. This means that until we can add support for them, you will manually have to create and maintain these relationships. That can only be done if you do the import manually as it is not possible to hook into the current JSON import.
Related
I am building an application in Android with multiple activities. I have a list of an object of type TodoItem that I get from a collection in Firestore database, and I need to access the list from more than one activity to make changes and updates to the list.
To do that, I thought about saving the list in the Application scope (is it a good idea?). For this reason, I created a class MyApplication extends Application (and added it to the Manifest file).
Instead of just adding the list as a class field of MyApplication I thought that maybe I should create a class named DataManager that will hold application-wide information such as my list of TodoItems (and here I ask again: is it a good idea? or maybe there is a better solution?).
At this point I am trying to decide what is a better approach to create and save the DataManager class:
One idea is to make DataManager a Singleton class and save it as a class field of MyApplication. This way, the activities will be able to get the instance of the class using DataManager.getInstance() without the need to get it from the application class with a getter method. In this approach, I will have to create the instance of DataManager and init the field of the application with it in the OnCreate() method of the application.
The second idea is to make it a non-singleton, add DataManager field to MyApplication, and create a getter named getDataManager() in the application class. The getter will check if the field is null (i.e. already initialized or not) and will create a new instance correspondingly. This way, the activities will get the instance using ((MyApplication) getApplication()).getDataManager().
I would like to hear what do you think about my approaches to solve the problem, and if you have any other suggestions or other ways to improve my suggested design.
A nice way when your data source is simple. You can create a singleton class to hold and manage data, including read and write from the singleton.
When you want to use complex data, you can store it to your device disk rather than memory. Android application support you to store your data with file, database, or key-value preference. As for your case, you can use database to store your todolist. Android support sqlite for these work, and we have official orm library called room.
raw sqlite: https://developer.android.com/training/data-storage/sqlite
room library: https://developer.android.com/topic/libraries/architecture/room
So I have a Game object that has an init block where I setup the object and upload to the Firebase Firestore. Then when I'm listening for changes in that object I have to convert the DocumentSnapshot to a Game object.
game = snapshot.toObject(Game::class.java)
Pretty simple. The problem is is that this calls the init block of my Game class and uploads another game object. Is there a way I can avoid calling the init block while doing this? Thanks!
When you use automatic field mapping like this, the convention is that you should use a class definition that contains only the fields you want to map, and nothing else. Objects that have only getters and setters for properties are called JavaBeans, and their sole purpose is to store data. These objects must define a default no-arg constructor.
If you have additional logic that works with your Game object, that should go in a different class. It's better design to keep your data separate from the logic that works with the data (as you have discovered).
Move your init code to a constructor, which you can call when you're creating an instance of your Game class and you want it to upload the game object.
I started using realm. it seemed to work fine, but I have some questions. When I use realm for simple object with primitive fields, everything is ok. But I'm facing issues using it for complex objects.
For example I have a class Passenger. It has several fields
Segment segment;
Documents documents;
....
Each field also has sub objects. Segment class
Flight flight;
Arrival arrival;
int pnrRequest;
So as I understand I will have several tables and I need one-to-many relations to connect this tables. What i want is to store passenger list inside database.
The problem is that I already have this classes as a model, but they dont extend RealmObject. I don't want to have duplicate classes one for model and one for database. Is there a way to avoid duplication of files and conversion from one model to another?
According to documentation it's posible:
An alternative to extending the RealmObject base class is implementing the RealmModel interface and adding the #RealmClass annotation.
Realm requires that all models that should be persisted must extend RealmObject or implement the interface RealmModel (see https://realm.io/docs/java/latest/#realmmodel-interface). If neither of these approaches work for you, you will need to duplicate the class and have conversion methods between them.
I have an object that i must save to file for reuse. The class of this object already implements Parcelable for use in intents. My knowledge of saving an object to file says to implement Serializable, but when i do, i get an error in the class that contains this object at the putExtra method of an intent because both Serializable and Parcelable have this method.
Is there a way to avoid this, or just a way that i can save my object state and reload it easily?
I have looked at a few articles and i feel no more informed about how i should be saving my object.
Thanks in advance
I believe that Parcelable and Serializable both reaches the same goal in different ways and with different performances. Given that, if some class in your object hierarchy alread implements the Parcelable interface, you can override its writeToParcel method, call the super for it (so the members of the super classes will be written to the parcel if they were implement that way) and then, you should write your attributes to the parcel, always keeping in mind that the order you use to save them is the order you will use to retrieve them latter (FILO data structure)
EDIT
Just cast your object where it complains and tells about the conflict to the class you want to use as described here: https://stackoverflow.com/a/13880819/2068693
I don't know that you can implement both Serializable and Parcelable together but for convert a class from Serializable to Parcelable you can use this plugin:
Android Parcelable Code generator.
First remove implement Serializable then with ALT + Insert and click on Parcelable you can generate your class.
You have options other than Serializable, but that may meet other requirements such as avoiding library dependencies. You can write objects to file using JSON or XML, which has the advantage of being readable. You may also need to consider versioning - what happens when you have files that need to be read by a class that contains a new field. Persistence brings with it some issues you probably don't have passing Bundles/Intents back and forth.
If you choose Serializable I'd recommend structuring your objects so they can be written to and read from a Bundle. Using a static MyObject.make(Bundle) method and an instance Bundle save() method keeps all the constants and read/write in a single location.
i have three classes and want to store all the data of three classes... these classes are composed in main class ... so can any one tell that whether i have implement the Serilzable interface on class or all three classes which are composed....
please check this out http://developer.android.com/guide/topics/data/data-storage.html
it is according to your requirement. but i suggest to use the shared preference if the data is in key pair value.it store value in xml tag inside the application and also fast operation as compare to other thing.
for shared preference here
All objects in the object tree of a Serializable object must implement Serializable; otherwise you'll get a NotSerializableException.
Serialization is for entire object graph or none. For e.g you have the following scenario
Class A
{
class b = new class B();
class c = new class C();
class d = new class D();
}
Suppose if you want to serialize A then all of the composed objects of different class has to implement serializable including A if any of composed object fails to implement serializable then NoSerializableException will be thrown.