1. Are you ready for the Galaxy S20? Here is everything we know so far!

Issue with comparing dates with Room persistence library

Discussion in 'Android Development' started by Damon Getsman, Jun 8, 2019.

  1. Damon Getsman

    Damon Getsman Newbie
    Thread Starter

    Hello everybody.

    I have another question regarding Room persistence library. I've been working with it successfully for awhile now, but due to the inability of SQLite to work with a LocalDateTime natively, I'm having some issues now.

    Not sure if this is directly relevant, due to the way that I'm handling things, but in my app, I'm saving a timestamp for the following object using the following type converters:
    Code (Text):
    2. @Entity
    3. public class Usage {
    4.     @PrimaryKey(autoGenerate = true)
    5.     private int             id;
    6.     @ColumnInfo(name="sub_id")
    7.     private int             sub_id;
    8.     @ColumnInfo(name="dosage")
    9.     private float           dosage;
    10.     @ColumnInfo(name="timestamp")
    11.     private LocalDateTime   timestamp;
    12.     @ColumnInfo(name="notes")
    13.     private String          notes;
    14. }
    (type converters follow):
    Code (Text):
    2.     @TypeConverter
    3.     public static LocalDateTime toLocalDateTime(long value) {
    4.         return LocalDateTime.ofEpochSecond(value, 0, ZoneOffset.UTC);
    5.     }
    7.     @TypeConverter
    8.     public static long fromLocalDateTime(LocalDateTime ldt) {
    9.         return ldt.toEpochSecond(ZoneOffset.UTC);
    10.     }
    In my app, I'm allowing the user to select a date via a Calendar (setting the hour, minute, second, and lower values all to 0) and then I'm trying to utilize a query to select all records prior to the date selected. I then utilize the Calendar.getTimeInMillis() method to attempt to convert this to a value that will be comparable to the epoch second value that is saved in the database with the following code:
    Code (Text):
    2. public void purgeDatabase(View v) {
    3.     if (validateData()) {
    4.         //now let's get down to the nitty gritty and delete the entries
    5.         GlobalMisc.showSimpleDialog(this, "Wiping entries",
    6.                 "Purging the database's records on the selected substance prior to the date " +
    7.                 "that you have selected.");
    8.     } else {
    9.         GlobalMisc.showSimpleDialog(this, "Not purging anything",
    10.                 "Not purging any administrations at this time.");
    11.     }
    13.     int countToFry = Permanence.getUsagePriorToCount(tmpCal.getTimeInMillis());
    14.     GlobalMisc.showSimpleDialog(this, "Toast Count",
    15.             "This would toast " + countToFry + " records from the database.");
    16. }
    Referenced code in Permanence is as follows:
    Code (Text):
    2. public class Permanence {
    3.     private static AppDatabase sDb;
    5.     public static int getUsagePriorToCount(long ts) {
    6.         return sDb.getUsageDao().getPriorToDateUsageCount(ts);
    7.     }
    8. }
    Referenced code via sDb.getUsageDao() is as follows:
    Code (Text):
    2. @Query("SELECT COUNT(*) FROM Usage WHERE timestamp > :ts")
    3. int getPriorToDateUsageCount(long ts);
    The behavior that I am seeing is that when my timestamp > :ts comparison is utilizing greater than (as here), I am getting a count of 0 entries via the query. When this is reversed I'm getting a count for all of the entries. So I'm thinking that either the long values that I'm trying to compare are not equivalent (epoch milliseconds is what I was shooting for w/Calendar.getTimeInMillis() and LocalDateTime.toEpochSecond()), or, well, something else that I don't have a clue about due to the fact that I'm not at any level of serious proficiency with Room or SQLite queries in general at this point.

    For brevity's sake, I have omitted portions of the classes referenced above, due to the fact that the boilerplate code is working fine with other methods in the same classes. I apologize if this is a faux pas, and will be more than happy to post more complete examples of the applicable code snippets. Along the same lines, I'm not really sure what else as far as code examples may be applicable to this problem; I think I've hit everything, but if I'm missing something else please let me know and I would be more than happy to post whatever hasn't been properly included. My apologies in advance if anything of the sort has been missed.

    I'd like to thank in advance anybody who reads this and especially those who are willing to help me troubleshoot a little bit farther in this process. I'm really not sure where to go with it at this point and I'm very grateful for anything that you may have to offer.

  2. Best Answer:
    Post #2 by 23tony, Jun 9, 2019 (1 points)
  3. 23tony

    23tony Well-Known Member

    I have recently learned that SQLite treats all integer types the same (internally, there isn't a LONG vs INT), but I don't know about Room.

    I assume you're not getting any errors. I would suggest adding a bunch of logging in your code to trace the values through the process and see where the problem occurs - at least that way you can narrow down your focus.
    Damon Getsman likes this.
  4. Damon Getsman

    Damon Getsman Newbie
    Thread Starter

    I ended up starting to log all of the variables in question and ended up determining that my error was somewhere else completely. Oops. Didn't really even think about logging things before, I didn't mean to be lazy about it. Guess I'd had a long break from coding and that probably had my skills down a bit for the count.

    Anyway, thank you for the advice, it ended up leading me to getting things working again, in a roundabout kind of way. :)
    23tony likes this.

Share This Page