Как иметь необязательный объект в классе при использовании Realm и API-интерфейс? (Android, дооснащение, Гсон) - PullRequest
0 голосов
/ 18 января 2020

У меня есть класс Event, который расширяет RealmObject, в котором есть еще один объект Location.

@Parcel(implementations = {EventRealmProxy.class}, value = Parcel.Serialization.FIELD, analyze = {Event.class})
public class Event extends RealmObject implements Serializable {

public static final String DATE_FIELD = "timestamp";
@PrimaryKey
@SerializedName("id")
public String id;
@SerializedName("date")
public String date;
@Nullable
public Date timestamp;
@SerializedName("image")
public String image;
@SerializedName("name")
public String name;

@Nullable
@SerializedName("event_clothing_items")
@ParcelPropertyConverter(RealmListParcelConverter.class)
public RealmList<ClothingItem> eventClothingItems;

@Nullable
@SerializedName("location")
public Location location;

@Nullable
@SerializedName("tags")
@ParcelPropertyConverter(RealmListParcelConverter.class)
public RealmList<Tag> tags;

public Event() {
    timestamp = new Date();
    eventClothingItems = new RealmList<>();
    location = new Location();
    tags = new RealmList<>();
}

public Event(Event event) {
    eventClothingItems = new RealmList<>();
    location = new Location();
    tags = new RealmList<>();
    this.id = event.id;
    this.date = event.date;
    this.image = event.image;
    this.name = event.name;
    this.tags.addAll(event.tags);
    this.eventClothingItems.addAll(event.eventClothingItems);
    this.location = new Location(event.location);

}


@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Event event = (Event) o;
    return id.equals(event.id);
}

@Override
public int hashCode() {
    return Objects.hash(id);
}
}

Вот класс Location:

@Parcel(implementations = {LocationRealmProxy.class}, value = Parcel.Serialization.FIELD, analyze = {Location.class})
public class Location extends RealmObject {

@PrimaryKey
@SerializedName("id")
public Integer id;
@SerializedName("city")
public String city;
@SerializedName("postal_code")
public String postalCode;
@SerializedName("state")
public String state;
@SerializedName("street")
public String street;

@Nullable
public Location() {
}

public Location(Location location) {
    this.id = location.id;
    this.city = location.city;
    this.postalCode = location.postalCode;
    this.state = location.state;
    this.street = location.street;
}
}

Приложение вылетает каждый раз, когда приходит Location null от API, здесь JSON от API,

{"event":{"id":125,"date":"2020-01-17 16:05:13 -0500","event_clothing_items":[],"event_tags":[],"image":null,"location":null,"name":"no location","tags":[]}}

Что я могу сделать, чтобы событие без местоположения и событие с местоположением тоже без сбоя приложения? Должен ли я использовать какой-то конвертер для десериализации его? пожалуйста помогите

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...