Search code examples
androiddatetimerealm

Android realm.io searching by datetime or unix timestamp returns null


What is proper way to query Realm db object using datetime or unix timestamp. I have made two queries, both of them doesn't work, just returns null:

protected void onTests() {

    // READ TV GUIDE info.
    RealmResults<ProgramObject> result = app.getDatabase().where(ProgramObject.class).findAll();
    ProgramObject programObject = result.where().equalTo("channel_id", 997).equalTo("time_start_unix", 1448563800000L).findFirst();
    String title = programObject.getTitle();

    Log.e("Print result: ", String.valueOf(title));

}

And, at result I will get NullpointerException because programObject is null How I understand, this part: .equalTo("time_start_unix", 1448563800000L) doesn`t work.

And my second query, where I`m passing datetime string "2015-11-26 22:30:00":

protected void onTests() {

    // READ TV GUIDE info.
    RealmResults<ProgramObject> result = app.getDatabase().where(ProgramObject.class).findAll();
    ProgramObject programObject = result.where().equalTo("channel_id", 997).equalTo("time_start", "2015-11-26 22:30:00").findFirst();
    String title = programObject.getTitle();

    Log.e("Print result: ", String.valueOf(title));

}

And at result I`m getting same as before, NullpointerException

There is my ProgramObject.class object:

public class ProgramObject extends RealmObject {

@PrimaryKey
private int id;
private int channel_id;

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

public void setChannel_id(int channel_id) {
    this.channel_id = channel_id;
}

public void setTime_start(String time_start) {
    this.time_start = time_start;
}

public void setTime_stop(String time_stop) {
    this.time_stop = time_stop;
}

public void setTitle(String title) {
    this.title = title;
}

public void setDescription(String description) {
    this.description = description;
}

public void setCreated(String created) {
    this.created = created;
}

public void setModified(String modified) {
    this.modified = modified;
}

public void setCategory1(String category1) {
    this.category1 = category1;
}

public void setCategory2(String category2) {
    this.category2 = category2;
}

public void setStatus(String status) {
    this.status = status;
}

public void setSys_status(String sys_status) {
    this.sys_status = sys_status;
}

public void setUrl(String url) {
    this.url = url;
}

public int getId() {
    return id;
}

public int getChannel_id() {
    return channel_id;
}

public String getTime_start() {
    return time_start;
}

public String getTime_stop() {
    return time_stop;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}

public String getCreated() {
    return created;
}

public String getModified() {
    return modified;
}

public String getCategory1() {
    return category1;
}

public String getCategory2() {
    return category2;
}

public String getStatus() {
    return status;
}

public String getSys_status() {
    return sys_status;
}

public Long getTime_start_unix() {
    return time_start_unix;
}

public Long getTime_stop_unix() {
    return time_stop_unix;
}

public void setTime_start_unix(Long time_start_unix) {
    this.time_start_unix = time_start_unix;
}

public void setTime_stop_unix(Long time_stop_unix) {
    this.time_stop_unix = time_stop_unix;
}

public String getUrl() {
    return url;
}

private String time_start;
private String time_stop;
private Long time_start_unix;
private Long time_stop_unix;
private String title;
private String description;
private String created;
private String modified;
private String category1;
private String category2;
private String status;
private String sys_status;
private String url;

}

And fragment from my json:

{
  "id": "1448400947631",
  "channel_id": "914",
  "time_start": "2015-12-01 09:35:00",
  "time_stop": "2015-12-01 10:00:00",
  "unix_start": "0",
  "unix_stop": "0",
  "title": "Jamie's 15 Minute Meals 1 E20",
  "description": "On today’s menu are Spiced chicken, bacon, asparagus and spinach lentils, plus Falafel wraps, grilled veg and salsa.",
  "created": "2015-11-26 07:34:33",
  "modified": "0000-00-00 00:00:00",
  "category1": "",
  "category2": "",
  "status": "0",
  "sys_status": "1",
  "url": ""
},

Solution

  • A similar issue is reported in https://github.com/realm/realm-java/issues/1839 and was resolved. The solution might also be applicable here as well.