Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room TypeConverters not recognized

I am trying to implement a DateConverter for persisting date. It's pretty generic, but I kept getting:

Cannot figure out how to save this field into database. You can consider adding a type converter for it.

I double checked and have the annotation defined at the database level and also at the field level, it still not able to resolve that there is a type converter defined for the field.

My grade file dependencies:

compile('android.arch.persistence.room:runtime:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
compile('android.arch.persistence.room:rxjava2:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
testCompile('android.arch.persistence.room:testing:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}
annotationProcessor('android.arch.persistence.room:compiler:1.0.0-alpha3') {
    exclude group: 'com.google.code.gson'
}

In my AppDatabase:

@Database(entities = {Location.class, Room.class, Amenity.class, UserModel.class, EventModel.class}, version =1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {

    public abstract RoomDao getRoomDao();
    public abstract LocationDao getLocationDao();
    public abstract AmenityDao getAmenityDao();
    public abstract UserDao getUserDao();
    public abstract EventDao getEventDao();

}

Then my DateConverter class looks like this:

public class DateConverter {

    @TypeConverter
    public static Date toDate(Long timestamp) {
        return timestamp == null ? null : new Date(timestamp);
    }

    @TypeConverter
    public static long toTimestamp(Date date) {
        return date == null ? null : date.getTime();
    }
}

My Entity looks like this:

@Entity(tableName = "event")
@TypeConverters({DateConverter.class})
public class EventModel {

    @PrimaryKey
    private String uuid;

    @ColumnInfo(name = "subject")
    private String subject;

    @TypeConverters({DateConverter.class})
    private Date start;

    @TypeConverters({DateConverter.class})
    private Date end;

    @ColumnInfo(name = "confirmed")
    private Boolean confirmed;

    public String getUuid() {return uuid;}
    public void setUuid(String uuid) {this.uuid = uuid;}

    public String getSubject() {return subject;}
    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Date getStart() {return start;}
    public void setStart(Date date) {this.start = date;}

    public Date getEnd() {return end;}
    public void setEnd (Date end) {
        this.end = end;

    }

    public Boolean getConfirmed() {return confirmed;}

    public void setConfirmed(Boolean confirmed) {
        this.confirmed = confirmed;
    }

}

Am I missing something still? Thanks!

like image 688
user8290985 Avatar asked Jul 12 '17 21:07

user8290985


2 Answers

Try changing:

public static long toTimestamp(Date date)

to:

public static Long toTimestamp(Date date)

Or, change the other one's parameter to long. IOW, have them use the same type.

For example, in this sample project, I use the following class, and it works fine:

/***
 Copyright (c) 2017 CommonsWare, LLC
 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.

 Covered in detail in the book _Android's Architecture Components_
 https://commonsware.com/AndroidArch
 */

package com.commonsware.android.room;

import android.arch.persistence.room.TypeConverter;
import java.util.Date;

public class TypeTransmogrifier {
  @TypeConverter
  public static Long fromDate(Date date) {
    if (date==null) {
      return(null);
    }

    return(date.getTime());
  }

  @TypeConverter
  public static Date toDate(Long millisSinceEpoch) {
    if (millisSinceEpoch==null) {
      return(null);
    }

    return(new Date(millisSinceEpoch));
  }
}
like image 190
CommonsWare Avatar answered Nov 04 '22 02:11

CommonsWare


The equivalent in Kotlin is: (at least the one I’m using)

class DateConverter {
    @TypeConverter
    fun toDate(timestamp: Long?): Date? {
        return when (timestamp) {
            null -> null
            else -> Date(timestamp)
        }
    }

    @TypeConverter
    fun toTimestamp(date: Date?): Long? {
        return date?.time
    }
}

Then you’d have your database:

@Database(entities = arrayOf(MyObject::class), version = 1)
@TypeConverters(DateConverter::class)
abstract class MyObjectDb : RoomDatabase() {
    abstract fun myObjectDao(): MyObjectDao
}
like image 5
Martin Marconcini Avatar answered Nov 04 '22 02:11

Martin Marconcini