MVC pattern with Firebase [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a simple question. I am developing an Android application that uses as backend Firebase realtime database. I was wondering if can it be said that my app is built following the MVC pattern, although I do not have my own logic, my own server, I am using the API from Firebase. Thanks!

Yes, you can use MVC pattern with Firebase. With Firebae it's even simpler. You can create a model class in which you can declare all the variables you need. This can include also other classes. Create all the constructors that you need. Add public setters and public getters and you'll have your complete model class or your POJO. Don't forget to add the no argument constructor needed for Firebase.
Here is an example of an user model class with only two fields.
public class UserModel implements Serializable {
private String userEmail;
private String userName;
public UserModel() {}
public void setUserEmail(String userEmail) {this.userEmail = userEmail;}
public String getUserEmail() {return userEmail;}
public void setUserName(String userName) {this.userName = userName;}
public String getUserName() {return userName;}
}
Every change that is made in your Firebase database is triggerd in real time in your user interface.
I recomand you reading the official documentation for Firebase. Here is how to add Firebase to your Android Project and here is how you can set up Firebase Realtime Database for Android. And here is how you can use FirebaseUI for Android.
Hope it helps.

Related

Autocomplete search result into Firebase [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
It might be novice question but I am trying to write a result of selected location from place autocomplete fragment (or from custom Autocomplete edittext) to firebase database. As per firebase's documentation java objects are automatically mapped.
But, I am getting an error that the object has to be serialized when I am trying write my object into firebase.
Here is the Stacktrace:
2020-03-07 02:24:47.149 8708-8708/com.smslunchdelivery.smslunchdelivery E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smslunchdelivery.smslunchdelivery, PID: 8708
com.google.firebase.database.DatabaseException: No properties to serialize found on class androidx.loader.app.LoaderManagerImpl
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.(com.google.firebase:firebase-database##19.2.0:547)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-database##19.2.0:329)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database##19.2.0:166)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database##19.2.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database##19.2.0:675)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database##19.2.0:167)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(com.google.firebase:firebase-database##19.2.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(com.google.firebase:firebase-database##19.2.0:675)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(com.google.firebase:firebase-database##19.2.0:167)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-database##19.2.0:60)
at com.google.firebase.database.DatabaseReference.setValueInternal(com.google.firebase:firebase-database##19.2.0:282)
at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database##19.2.0:159)
at com.smslunchdelivery.smslunchdelivery.FoodActivity.pushObjects(FoodActivity.java:237)
at com.smslunchdelivery.smslunchdelivery.FoodActivity.access$100(FoodActivity.java:48)
at com.smslunchdelivery.smslunchdelivery.FoodActivity$2.addToCardBtnClck(FoodActivity.java:175)
at com.smslunchdelivery.smslunchdelivery.FoodAdapter$4.onClick(FoodAdapter.java:113)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
My Object
public class User {
public Fragment placeAutoCompleteFragment;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(Fragment placeAutoCompleteFragment, String email) {
this.placeAutoCompleteFragment = placeAutoCompleteFragment;
this.email = email;
}
}
private void (String userId, String name, String email) {
//Getting error message here.
User user = new User(placeAutoCompleteFragment, email);
mDatabase.child("users").child(userId).setValue(user);
}
Firebase Documentation
private void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);
mDatabase.child("users").child(userId).setValue(user);
}
My question is: What is the best possible way to achieve this?
Your User class is not suitable for automatic serialization to Firestore. The Fragment member is causing problems. A class you want to serialize should only contain primitive types, lists and maps, and other objects that only contain the same types of data. At the very minimum, remove the Fragment and find another way to deal with it outside of your POJO.
Firebase's documentation java objects are automatically mapped but the problem in here is that Fragment is an Android object, so it doesn't know how to serialize it.
Your data objects should be POJO (Plain Old Data Object) to be serialized and deserialized, there is no place for a Fragment in there or other android objects.

What is the ORM for Android with support #Inheritance(strategy = InheritanceType.SINGLE_TABLE)?

I am developing an android chat application, I have several types of messages inherited from a single abstract class. I want to get a list of different types of chat messages. I think I need an ORM with inheritance support with a SINGLE_TABLE strategy. Is there an ORM for Android with support for this functionality? Or, perhaps, you will advise how to solve this problem using the ORM without SINGLE_TABLE support?
Examples:
public abstract class AbstractMessage implements MessageListContent, Serializable, Comparable<AbstractMessage> {
public enum Status {
DELIVERED,
SENDING_AND_VALIDATION,
NOT_SENDED,
INVALIDATED
}
private SupportedMessageListContentType supportedType = SupportedMessageListContentType.UNDEFINED;
private boolean iSay;
private long timestamp;
private Status status = Status.SENDING_AND_VALIDATION;
private String transactionId;
private String companionId;
// getters and setters
//...
}
public class BasicMessage extends AbstractMessage {
private String text;
private transient Spanned htmlText;
// getters and setters
//...
}
public class TransferMessage extends AbstractMessage {
private BigDecimal amount;
// getters and setters
//...
}
I don't know if you know a lot about ORM's in android, but, two of the most famous ORM's for Android are Room and Realm. And these two could achieve what you want. But, don't take my word on realm, as I am only going to state what my friend told me about realm.
For starters, Room runs in SQLite and Realm in NoSQL. Now, I assume that you know greatly about inheritance and polymorphism, this concept could also be applied to SQL. This is, taking in account that you choose SQLite. Now for realm, it is a different story tho. My friend told me that the polymorphism of your models answers to the polymorphism of the database. Though, I highly doubt that, but I don't like realm, so don't take my word for it.
For choosing your database, I will be frank to tell you to choose SQLite and to help you decide, here is a simple site that currated reasons on which is better, SQL or NoSQL: http://www.nosql-vs-sql.com/

Android Room partly update if exit

I ran across a problem where I am not really sure how to solve it. The project I am working on currently has a model which partly consists of backend stored data and data from the local database.
So what I am trying to Archive is something like that:
Article : [Bunch of Information] & [boolean Subscribed]
The subscribed field is device bound and should not reflect any data on the backend. My question is if it is possible to implement in Room some kind of createIfNotExit() Method that handles the following cases:
Article not present locally: store a copy and set Subscribed to
false
Article present: update all the Information and Keep the
Subscribe-Flag untouched
My idea is to split the model into a separate Subscription-Model holding a reference to the Article. This way I could implement it simply via #Update(OnConfict=Update) etc...
Is there a way to implement a simple #Query method in the DAO that performs what I want?
Sorry if this is a really basic question but I couldn't find any material about best practices handling this case.
Thank you in advance!
For example, your entity is:
#Entity(tableName = "articles")
public final class Article {
#PrimaryKey
public long serverId;
public String title;
public String url;
public boolean isSubscribed;
}
You may write this method in DAO:
#Query("INSERT OR REPLACE INTO articles (serverId, title, url, isSubscribed) VALUES (:id, :title, :url,
COALESCE((SELECT isSubscribed FROM articles WHERE id = :id), 0));")
void insertOrUpdateArticle(long id, String title, String url);
Another option - write this logic in your repository and use two simple operations: select and update

MediaBrowser(Compat) subscribe/query items with pagination [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In my Service-side, it has huge data and I want to provide items with pagination way.
Is there any recommended flow to traverse with pagination between MediaBrowser/MediaBrowserService(Compat)?
I want to provide partial data in Result<List<MediaItem>> instead of all data (e.g. all YouTube songs), while browser-side using pagination to pull partial data once a time.
In your
MediaService extends MediaBrowserServiceCompat
#Override
public void onLoadChildren(#NonNull final String parentMediaId, #NonNull final Result<List<MediaItem>> result) {
result.detach();
for (int page = 0; i<pages.size(); i++){
result.sendResult(getList(page));
}
}
public List<MediaItem> getList(int page){
//here create List for page-number == page
}
OR
You can make request in your Fragment or Activity with page
MediaBrowserCompat mediaBrowser = ...;
mediaBrowser.subscribe("1"/*it's page*/, mSubscriptionCallback);
then in your Service make this:
#Override
public void onLoadChildren(#NonNull final String page, #NonNull final Result<List<MediaItem>> result) {
result.detach();
result.sendResult(getList(page));
}
I wanted to do a similar thing in my app - I wanted to return the existing songs on the device but in paginated way. Like you say, a partial result in the onLoadChildren() method. I ended up using Android's new Paging library to do just that. Using the library, I could make the client/UI side asks the service for only the pages the user is interested to see, and then serve only those in the onLoadChildren() method, calling the client's subscribe() method to retrieve it.
I go over it in details in a post I wrote, where I also give code samples to better demonstrate the concept.

Can an object in Realm return its containing object in a query?

eg.
class Question extends RealmObject
{
Answer answer;
}
_
class Answer extends RealmObject
{
String ding;
}
In a query can an Answer object link to the Question that holds it? Or do I have to create a new field in Answer that links to the Question that contains it?
The feature you are looking for is called backlinks, but isn't implemented yet. You can follow progress here https://github.com/realm/realm-java/issues/607, but currently you will have to create and maintain a bidirectional link to be able to query the container object.

Categories

Resources