Dex Sebas Dex Sebas - 3 months ago 33
Android Question

Android - Realm objects

UPDATED:

I created a new class, check the complet code:
TEST Class:

import org.parceler.Parcel;

import io.realm.RealmObject;
import io.realm.TESTRealmProxy;
import io.realm.annotations.PrimaryKey;

@Parcel(implementations = {TESTRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {TEST.class})
public class TEST extends RealmObject {

@PrimaryKey
int Id;

String Name;

public int getId() {
return Id;
}

public String getName() {
return Name;
}

public TEST(){

}

public TEST(int id, String name){
this.Id = id;
this.Name = name;
}
}


Activity1:

Intent resultIntent = new Intent(this, NewActivity.class);
TEST dd = new TEST(2, "pa7");
Log.e(TAG, "Profile: "+dd.getId());
resultIntent.putExtra("userProfile",Parcels.wrap(dd));
startActivityForResult(resultIntent, 3);


NewActivity:

TEST mUserd = Parcels.unwrap(getIntent().getParcelableExtra("userProfile"));


if(mProfileUserd != null) {
Log.e(TAG, "ID: USER: " + mUserd.getId()+" - name "+mUserd.getName());
} else {
Log.e(TAG, "Is Null");
}


OUTPUT:

Profile: 2
ID: USER: 0 - name null


OLD

User class:

@Parcel(implementations = {
UserRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {User.class})
public class User extends RealmObject {

int Id;

String Name;

}


Function:

Intent resultIntent = new Intent(this, NewActivity.class);
resultIntent.putExtra("userProfile",Parcels.wrap(User.class, mAuthor));
startActivityForResult(resultIntent, 6);


NewActivity:

mUser = Parcels.unwrap((Parcelable) getIntent().getExtras().get("userProfile"));


I have my Object called User, everything works okay besides one thing. I have one fragment(B) that request users from database but I don't need to save that users in realm. The only thing that I need in this fragment is send to another activity the user object.

Note: In other fragment (A) I do need save the users but in fragment B I don't.

My question: Do I need to create another Object called "User2" or something that not extends RealmObject and use in fragment B? Or there is another way.

public class User extends RealmObject {
}

Answer

Two options:

1.) save to DB and query based on ID

2.) use Parceler library and implement Parcelable

@Parcel(implementations = { UserRealmProxy.class },
        value = Parcel.Serialization.BEAN,
        analyze = { User.class })
public class User extends RealmObject {
    // ...
}

compile "org.parceler:parceler-api:1.1.5"
apt "org.parceler:parceler:1.1.5"

To parcel RealmList, use following code

/* Copyright 2016 Patrick Löwenstein
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

public class RealmListParcelConverter implements TypeRangeParcelConverter<RealmList<? extends RealmObject>, RealmList<? extends RealmObject>> {
  private static final int NULL = -1;

  @Override
  public void toParcel(RealmList<? extends RealmObject> input, Parcel parcel) {
    if (input == null) {
      parcel.writeInt(NULL);
    } else {
      parcel.writeInt(input.size());
      for (RealmObject item : input) {
        parcel.writeParcelable(Parcels.wrap(item), 0);
      }
    }
  }

  @Override
  public RealmList fromParcel(Parcel parcel) {
    int size = parcel.readInt();
    RealmList list = new RealmList();

    for (int i=0; i<size; i++) {
      Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader());
      list.add((RealmObject) Parcels.unwrap(parcelable));
    }

    return list;
  }
}

Normally I'd tell you to just copyFromRealm() and send it over, but you have to somehow serialize it anyways, and you'll need Parcelable for that.

EDIT: Okay I've tried and tested the following:

@Parcel(implementations = { PostRealmProxy.class },
        value = Parcel.Serialization.BEAN,
        analyze = { Post.class })
public class Post extends RealmObject {
    @PrimaryKey
    private long id;

    private String text;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

public void startNextActivity(View view) {
    Log.i("MAINACTIVITY", "START NEXT ACTIVITY");
    Post post = new Post();
    post.setId(41274);
    post.setText("Blaaaaaah");
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("post", Parcels.wrap(post));
    startActivity(intent);
}

public class SecondActivity
        extends RealmActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...
        Post post = Parcels.unwrap(getIntent().getParcelableExtra("post"));
        Log.i("SECOND", "" + post.getId());
        Log.i("SECOND", "" + post.getText());
    }
}

Output says:

09-05 23:54:09.343 12085-12085/com.zhuinden.realmdatabind I/MAINACTIVITY: START NEXT ACTIVITY
09-05 23:54:09.383 12085-12085/com.zhuinden.realmdatabind I/SECOND: 41274
09-05 23:54:09.383 12085-12085/com.zhuinden.realmdatabind I/SECOND: Blaaaaaah