How to avoid duplicate data in Firebase Database - android

In my Android application I am inserting a VideoEntity objects in firebase database this way:
#Override
protected void onCreate(Bundle savedInstanceState){
Log.d(TAG, " onCreate(Bundle) - Ini ");
super.onCreate(savedInstanceState);
database = FirebaseDatabase.getInstance();
String videoId = "";
for(VideoEntity video: videosList) {
videoId = video.getId();
DatabaseReference mRef = database.getReference().child("Videos").push();
mRef.setValue(video);
}
this the VideoEntity class:
package com.app.entities;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
public class VideoEntity implements Parcelable{
private String id;
private String title;
private String description;
private String thumbnailURL;
private String category;
public VideoEntity() {
}
public VideoEntity(Parcel in) {
id = in.readString();
title = in.readString();
description = in.readString();
thumbnailURL = in.readString();
category = in.readString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnail) {
this.thumbnailURL = thumbnail;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {return category;}
public void setCategory(String category) { this.category = category;}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(title);
dest.writeString(description);
dest.writeString(thumbnailURL);
dest.writeString(category);
}
public static final Creator<VideoEntity> CREATOR = new Creator<VideoEntity>() {
#Override
public VideoEntity createFromParcel(Parcel in) {
return new VideoEntity(in);
}
#Override
public VideoEntity[] newArray(int size) {
return new VideoEntity[size];
}
};
}
The problem is that everytime I start this activity the same objects are inserted in the database, so i have duplicate data in the database.
Is there anyway to avoid this ?

If your data has a natural key, store it under its natural key. In your case the videos have an id field, which likely is unique already. So instead of storing the videos under a push ID, store them under their existing ID:
DatabaseReference mRef = database.getReference().child("Videos").child(video.getId());
mRef.setValue(video);
That way the next time you run the app, it will just write the same data in the same location again.

Related

How to insert RealmList to RealmDB

I am trying to insert realm lists to my realmdb in my first insert there is no problem but when I insert another realmList with a different class, my old values are deleted. So how can I insert a realmlist to DB? inserts are done separately.
RealmList<Product> insertedProduct = new RealmList<Product>();
RealmList<ProductSubCategory> insertedSubCategory = new RealmList<ProductSubCategory>();
RealmList<ProductMainCategory> insertedMainCategory = new RealmList<ProductMainCategory>();
... inserting to realm list firt time ...
insertRealmList(insertedProduct); //inserting to realmDB first time
insertRealmList(insertedSubCategory); //inserting to realmDB first time
insertRealmList(insertedMainCategory); //inserting to realmDB first time
first insert to realmDB
RealmList insert function
private void insertRealmList(final RealmList realmObject) {
mRealm = Realm.getDefaultInstance();
realmTask = mRealm.executeTransactionAsync(
new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(realmObject);
}
},
new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
Log.i(TAG, "onSuccess: successfully inserted data");
}
},
new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
Log.i(TAG, "onError: error while inserting " + "error: " + error);
}
}
);
}
... inserting to realm list second time...
insertRealmList(insertedLanguages); //insert realmDB second time
second insert to realmDB
PRODUCT MODEL
package Model;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.Index;
import io.realm.annotations.PrimaryKey;
public class Product extends RealmObject {
#PrimaryKey
#Index
private int ID;
#Index
private int PLU_NO;
private String PRODUCT_NAME;
private String MAIN_CATEGORY;
private String SUB_CATEGORY;
private String TYPE;
private String TUS;
private Double PRICE;
private String CALORIE;
private String COOKING_TIME;
private RealmList<String> PRODUCT_NAME_LANGUAGES;
private RealmList<String> PRODUCT_DESCRIPTION_LANGUAGES;
private String IMAGE_BASE_HORIZONTAL; //4/3
private String IMAGE_BASE_VERTICAL; //16/9
private String VIDEO_NAME; //video name
#Ignore
private int AMOUNT;
#Ignore
private int WINDOW_AMOUNT = 1;
public Product(int ID, int PLU_NO, String PRODUCT_NAME, String MAIN_CATEGORY, String SUB_CATEGORY, String TYPE, String TUS, Double PRICE, String CALORIE, String COOKING_TIME, RealmList<String> PRODUCT_NAME_LANGUAGES, RealmList<String> PRODUCT_DESCRIPTION_LANGUAGES, String IMAGE_BASE_HORIZONTAL, String IMAGE_BASE_VERTICAL, String VIDEO_NAME) {
this.ID = ID;
this.PLU_NO = PLU_NO;
this.PRODUCT_NAME = PRODUCT_NAME;
this.MAIN_CATEGORY = MAIN_CATEGORY;
this.SUB_CATEGORY = SUB_CATEGORY;
this.TYPE = TYPE;
this.TUS = TUS;
this.PRICE = PRICE;
this.CALORIE = CALORIE;
this.COOKING_TIME = COOKING_TIME;
this.PRODUCT_NAME_LANGUAGES = PRODUCT_NAME_LANGUAGES;
this.PRODUCT_DESCRIPTION_LANGUAGES = PRODUCT_DESCRIPTION_LANGUAGES;
this.IMAGE_BASE_HORIZONTAL = IMAGE_BASE_HORIZONTAL;
this.IMAGE_BASE_VERTICAL = IMAGE_BASE_VERTICAL;
this.VIDEO_NAME = VIDEO_NAME;
}
public Product() {
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public int getPLU_NO() {
return PLU_NO;
}
public void setPLU_NO(int PLU_NO) {
this.PLU_NO = PLU_NO;
}
public String getPRODUCT_NAME() {
return PRODUCT_NAME;
}
public void setPRODUCT_NAME(String PRODUCT_NAME) {
this.PRODUCT_NAME = PRODUCT_NAME;
}
public String getMAIN_CATEGORY() {
return MAIN_CATEGORY;
}
public void setMAIN_CATEGORY(String MAIN_CATEGORY) {
this.MAIN_CATEGORY = MAIN_CATEGORY;
}
public String getSUB_CATEGORY() {
return SUB_CATEGORY;
}
public void setSUB_CATEGORY(String SUB_CATEGORY) {
this.SUB_CATEGORY = SUB_CATEGORY;
}
public String getTYPE() {
return TYPE;
}
public void setTYPE(String TYPE) {
this.TYPE = TYPE;
}
public String getTUS() {
return TUS;
}
public void setTUS(String TUS) {
this.TUS = TUS;
}
public Double getPRICE() {
return PRICE;
}
public void setPRICE(Double PRICE) {
this.PRICE = PRICE;
}
public String getCALORIE() {
return CALORIE;
}
public void setCALORIE(String CALORIE) {
this.CALORIE = CALORIE;
}
public String getCOOKING_TIME() {
return COOKING_TIME;
}
public void setCOOKING_TIME(String COOKING_TIME) {
this.COOKING_TIME = COOKING_TIME;
}
public RealmList<String> getPRODUCT_NAME_LANGUAGES() {
return PRODUCT_NAME_LANGUAGES;
}
public void setPRODUCT_NAME_LANGUAGES(RealmList<String> PRODUCT_NAME_LANGUAGES) {
this.PRODUCT_NAME_LANGUAGES = PRODUCT_NAME_LANGUAGES;
}
public RealmList<String> getPRODUCT_DESCRIPTION_LANGUAGES() {
return PRODUCT_DESCRIPTION_LANGUAGES;
}
public void setPRODUCT_DESCRIPTION_LANGUAGES(RealmList<String> PRODUCT_DESCRIPTION_LANGUAGES) {
this.PRODUCT_DESCRIPTION_LANGUAGES = PRODUCT_DESCRIPTION_LANGUAGES;
}
public String getIMAGE_BASE_HORIZONTAL() {
return IMAGE_BASE_HORIZONTAL;
}
public void setIMAGE_BASE_HORIZONTAL(String IMAGE_BASE_HORIZONTAL) {
this.IMAGE_BASE_HORIZONTAL = IMAGE_BASE_HORIZONTAL;
}
public String getIMAGE_BASE_VERTICAL() {
return IMAGE_BASE_VERTICAL;
}
public void setIMAGE_BASE_VERTICAL(String IMAGE_BASE_VERTICAL) {
this.IMAGE_BASE_VERTICAL = IMAGE_BASE_VERTICAL;
}
public String getVIDEO_NAME() {
return VIDEO_NAME;
}
public void setVIDEO_NAME(String VIDEO_NAME) {
this.VIDEO_NAME = VIDEO_NAME;
}
public int getAMOUNT() {
return AMOUNT;
}
public void setAMOUNT(int AMOUNT) {
this.AMOUNT = AMOUNT;
}
public int getWINDOW_AMOUNT() {
return WINDOW_AMOUNT;
}
public void setWINDOW_AMOUNT(int WINDOW_AMOUNT) {
this.WINDOW_AMOUNT = WINDOW_AMOUNT;
}
public void setDEFAULT_WINDOW_AMOUNT() {
this.WINDOW_AMOUNT = 1;
}
}
LANGUAGE MODEL
package Model;
import io.realm.RealmObject;
public class Language extends RealmObject {
#PrimaryKey
#Index
private int ID;
private String ICON;
private String NAME;
public Language(){
}
public Language(int ID, String ICON, String NAME) {
this.ID = ID;
this.ICON = ICON;
this.NAME = NAME;
}
public int getId() {
return ID;
}
public void setId(int id) {
this.ID = id;
}
public String getICON() {
return ICON;
}
public void setICON(String ICON) {
this.ICON = ICON;
}
public String getName() {
return NAME;
}
public void setName(String name) {
this.NAME = name;
}
}
Other two models are similar with product.
Please try the following method:
private <T extends RealmModel> void insertRealmList(final List<T> objects) {
try (Realm realmInstance = Realm.getDefaultInstance()) {
realmInstance.executeTransaction(realm -> {
for (T object : objects) {
realmInstance.insertOrUpdate(object);
}
});
}
}
Hope this helps!

Android getParcelableExtra returns null

I am currently trying to pass some data from my fragment to an activity. I am passing some parameter, and also an arraylist of a model class. I already make sure that the arraylist before I passed it is not null, since I am able to print out the value in log. But it become null in the called activity.
Here is the model class:
public class Product implements Parcelable {
String code, id, status, is_expired;
public Product() {}
public Product(String code, String id, String status, String is_expired) {
this.code = code;
this.id = id;
this.status = status;
this.is_expired = is_expired;
}
public Product(Parcel in) {
code = in.readString();
id = in.readString();
status = in.readString();
is_expired = in.readString();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIs_expired() {
return is_expired;
}
public void setIs_expired(String is_expired) {
this.is_expired = is_expired;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(code);
dest.writeString(id);
dest.writeString(status);
dest.writeString(is_expired);
}
public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>() {
public Product createFromParcel(Parcel in) {
return new Product(in);
}
public Product[] newArray(int size) {
return new Product[size];
}
};
}
Here is the caller:
intent.putExtra("product", productList);
Here is how retrieve the data:
ArrayList<Product> productList = getIntent().getParcelableExtra("product");
What am I doing wrong here?
Try this .
intent.putParcelableArrayListExtra("product", productList);
And next Activity .
ArrayList<Product> productList = getIntent().getParcelableArrayListExtra("product");

Put serializable into intent not working

I put in intent my object with following code (while putting list does exists, I checked in debugger)
Intent intent = new Intent(getApplicationContext(), ProductActivity.class);
intent.putExtra("product", CategoryActivity.this.list.get(position));
startActivity(intent);
And then retrive
Intent intent = getIntent();
product = (Product) intent.getSerializableExtra("product");
But it is null
Model class is following
public class Product implements Serializable {
Integer pk;
String name;
String description;
Integer price;
String image_url;
List<Option> options;
public Product(Integer pk, String name, String description, Integer price, String image_url, List<Option> options) {
this.pk = pk;
this.name = name;
this.description = description;
this.price = price;
this.image_url = image_url;
this.options = options;
}
public Integer getPk() {
return pk;
}
public void setPk(Integer pk) {
this.pk = pk;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public List<Option> getOptions() {
return options;
}
public void setOptions(List<Option> options) {
this.options = options;
}
}
What am I doing wrong ?
You have to add more code to use your class inside intent data.
import android.os.Parcel;
import android.os.Parcelable;
public class Yourclass implements Parcelable {
//your code
#Override
public void writeToParcel(Parcel out, int flags) {
//e.g.
out.writeInt(this.getWhat());
//similar code
}
#Override
public int describeContents() {
return 0;
}
private YourClass(Parcel in) {
//e.g.
this.setWhat(in.readInt());
}
public static final Parcelable.Creator<YourClass> CREATOR
= new Parcelable.Creator<YourClass>() {
public YourClass createFromParcel(Parcel in) {
return new YourClass(in);
}
public YourClass[] newArray(int size) {
return new YourClass[size];
}
};
}
to store use intent.putExtra(EXTRANAME, obj); to retrieve use YourClass myObj = (YourClass) getIntent().getParcelableExtra(EXTRANAME);

What is the correct way of creating a parcelable?

I have created the following parcelable object:
public class ViewModel implements Parcelable {
private String image, price, credit, title, description, id;
public ViewModel(String image, String price, String credit, String title, String description, String id) {
this.image = image;
this.price = price;
this.credit = credit;
this.title = title;
this.description = description;
this.id = id;
}
public String getPrice() {
return price;
}
public String getCredit() {
return credit;
}
public String getDescription() {
return description;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] {
this.image,
this.price,
this.credit,
this.title,
this.description,
this.id
});
}
/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<ViewModel> CREATOR = new Parcelable.Creator<ViewModel>() {
public ViewModel createFromParcel(Parcel pc) {
return new ViewModel(pc);
}
public ViewModel[] newArray(int size) {
return new ViewModel[size];
}
};
/**Creator from Parcel, reads back fields IN THE ORDER they were written */
public ViewModel(Parcel pc){
image = pc.readString();
price = pc.readString();
credit = pc.readString();
title = pc.readString();
description = pc.readString();
id = pc.readString();
}
}
Now I am sending an ArrayList of ViewModel through bundle:
bundle.putParcelableArrayList("products", viewModels);
Is there anything wrong I am doing? Because I get null Bundle Arguments, but if I send a simple string, everything works.
make this change:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.image);
dest.writeString(this.price);
dest.writeString(this.credit);
dest.writeString(this.title);
dest.writeString(this.description);
dest.writeString(this.id);
}
getIntent().getParcelableArrayListExtra("product"); // get the list
You are trying to retrieve a parcelable ArrayList. You need to retrive a parcelable ViewModel object that you created. Change:
bundle.putParcelableArrayList("product", viewModels);
and
getIntent().getParcelableArrayList("product");
to:
bundle.putParcelable("product", viewModels);
and
getIntent().getParcelable("product");

Android: OutOfMemoryError when unmarshalling a Parcelable

i got a serious problem while trying to unmarshall an ArrayList of custom objects, with an ArrayList of custom objects as a field in it. When i remove everything that has to do with the ArrayList<User> in the Coon.java everything works fine. The error occurs, after including the "readTypedList"-part....Trying to fix that problem the whole day and can't find any solution.
Thanx in advance,
Roberto
Stacktrace: http://pastebin.com/LAGYmW95
Code:
-> Coon.java <-
package development.coonDub.misc;
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class Coon implements Parcelable {
/**
*
*/
public Coon(Parcel in) {
readFromParcel(in);
}
String name;
String city;
String country;
ArrayList<User> pts = new ArrayList<User>();
public Coon(String name, String city, String country,ArrayList<User> pts) {
this.name = name;
this.city = city;
this.country = country;
this.pts = pts;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public ArrayList<User> getPts() {
return pts;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(city);
dest.writeString(country);
dest.writeTypedList(pts);
}
private void readFromParcel(Parcel in) {
name = in.readString();
city = in.readString();
country = in.readString();
in.readTypedList(pts, User.CREATOR);
}
public static final Parcelable.Creator<Coon> CREATOR =
new Parcelable.Creator<Coon>() {
public Coon createFromParcel(Parcel in) {
return new Coon(in);
}
public Coon[] newArray(int size) {
return new Coon[size];
}
};
}
-> User.java <-
package development.coonDub.misc;
import android.os.Parcel;
import android.os.Parcelable;
public class User implements Parcelable {
String name;
public static final Parcelable.Creator<User>
CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}
public User[] newArray(int size) {
return new User[size];
}
};
public User(String name) {
this.name = name;
}
public User(Parcel in) {
readFromParcel(in);
}
private void readFromParcel(Parcel in) {
name = in.readString();
}
public String getName() {
return name;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}
}
-> Activity1.class <-
...
Intent notificationIntent =
new Intent(getApplicationContext(), Activity2.class);
// coons is an ArrayList<Coon>..
notificationIntent.putExtra("coons", coons);
...
-> Activity2.class <-
...
ArrayList<Coon> coons = getIntent().getExtras().getParcelableArrayList("coons");
...

Categories

Resources