mahdi pishguy
mahdi pishguy

Reputation: 1034

Android Realm relationShip return null

I have model class as RealmObject which called ProductsDetailInformation, in this model i have single field as

private RealmList<ProductSlideShowImages> slideShowImages;

which i want to get this product images from ProductSlideShowImages model, but my this test return [0], for example:

ProductsDetailInformation = [
    {id: 57f72549-53e1-4177-9e52-80b8421559b7}, 
    {marketId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a}, 
    {productId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a-123456}, 
    {weight: 10}, 
    {cost: 1000}, 
    {color: blue}, 
    {madeIn: china}, 
    {gender:}, 
    {sex:}, 
    {quality: low}, 
    {size: 42}, 
    {sizeModel: xxl}, 
    {count: 10}, 
    {rating: 2}, 
    {description: my description}, 
    {created_at: 2016 - 07 - 17}, 
    {updated_at: 2016 - 07 - 14}, 
    {slideShowImages: RealmList < ProductSlideShowImages > [0]}
]

in both of them marketId and productId has same data.

ProductsDetailInformation class:

public class ProductsDetailInformation extends RealmObject{

    @PrimaryKey
    private String id;
    private String marketId;
    private String productId;
    private String weight;
    private String cost;
    private String color;
    private String madeIn;
    private String gender;
    private String sex;
    private String quality;
    private String size;
    private String sizeModel;
    private String count; 
    private String rating;
    private String description;
    private String created_at;
    private String updated_at;

    /**
     * RelationShip columns with ProductSlideShowImages table
     */
    private RealmList<ProductSlideShowImages> slideShowImages;

    /*SETTER AND GETTER*/
}

ProductSlideShowImages class:

public class ProductSlideShowImages extends RealmObject{
    @PrimaryKey
    private String id;
    private String marketId;
    private String productId;
    private String imageFileName;
    private String created_at;
    private String updated_at;

    /*SETTER AND GETTER*/
}

Get result with query:

List<ProductsDetailInformation> productsDetails = 
realm.where(ProductsDetailInformation.class).equalTo("marketId",marketUUid).findAll();

ProductSlideShowImages model and ProductsDetailInformation doesnt fill together, they are fill on difference place and time

EDIT:

this below trasaction i save information to ProductSlideShowImages model

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        ProductSlideShowImages slideShowImages = new ProductSlideShowImages();
        try {
            //@formatter:off
                slideShowImages.setId            ( UUID.randomUUID().toString()              );
                slideShowImages.setMarketId      ( jsonObject.getString ( "marketId"         ));
                slideShowImages.setProductId     ( jsonObject.getString ( "productId"        ));
                slideShowImages.setImageFileName ( jsonObject.getString ( "imageFileName"    ));
                slideShowImages.setCreated_at    ( jsonObject.getString ( "created_at"       ));
                slideShowImages.setUpdated_at    ( jsonObject.getString ( "updated_at"       ));
            //@formatter:on
            realm.copyToRealm(slideShowImages);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
});

and with below transaction in other method i save data to ProductsDetailInformation model:

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        ProductsDetailInformation products = new ProductsDetailInformation();
        try {
            //@formatter:off
                products.setId            ( UUID.randomUUID().toString()            );
                products.setMarketId      ( jsonObject.getString ( "marketId"     ));
                products.setProductId     ( jsonObject.getString ( "productId"     ));
                products.setWeight        ( jsonObject.getString ( "weight"       ));
                products.setCost          ( jsonObject.getString ( "cost"         ));
                products.setColor         ( jsonObject.getString ( "color"        ));
                products.setMadeIn        ( jsonObject.getString ( "madeIn"       ));
                products.setGender        ( jsonObject.getString ( "gender"       ));
                products.setSex           ( jsonObject.getString ( "sex"          ));
                products.setQuality       ( jsonObject.getString ( "quality"      ));
                products.setSize          ( jsonObject.getString ( "size"         ));
                products.setSizeModel     ( jsonObject.getString ( "sizeModel"    ));
                products.setCount         ( jsonObject.getString ( "count"        ));
                products.setRating        ( jsonObject.getString ( "rating"       ));
                products.setDescription   ( jsonObject.getString ( "description"  ));
                products.setCreated_at    ( jsonObject.getString ( "created_at"   ));
                products.setUpdated_at    ( jsonObject.getString ( "updated_at"   ));
            //@formatter:on
            realm.copyToRealm(products);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Error ", " parse json objects");
        }
    }
});

Upvotes: 0

Views: 295

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

You must set your relationships manually.

    ProductsDetailInformation products = new ProductsDetailInformation();
    products.setSlideShowImages(new RealmList<ProductSlideShowImages>());
    try {
        ...
        RealmResults<ProductSlideShowImages> slideShows = realm
            .where(ProductSlideShowImages.class)
            .equalTo("marketId", products.getMarketId())
            .findAll();
        for(ProductSlideShowImages slideShow : slideShows) { //only 0.89.0+
            products.getSlideShowImages().add(slideShow);
        }
        realm.copyToRealm(products);

Upvotes: 2

Related Questions