huzit
___을 입력해주세요
huzit
전체 방문자
  • 분류 전체보기 (137)
    • 안드로이드(Compose) (10)
      • UI (4)
      • 개념 (6)
    • 안드로이드 (50)
      • 기본개념 (6)
      • 응용 (4)
      • Debug (18)
      • Binding (3)
      • RecyclerView (5)
      • Firebase (6)
      • Retrofit (1)
      • Activity & Fragment (4)
    • 코틀린 (22)
    • 코딩테스트 (38)
      • 백준 (10)
      • 프로그래머스 (28)
    • 일상 (6)
    • CS 지식 (4)
    • 라즈베리파이 (7)

블로그 메뉴

  • 홈
  • 태그
  • 글쓰기
  • 관리

공지사항

인기 글

태그

  • Retrofit
  • RecyclerView
  • Java
  • Debug
  • compose
  • Android
  • FCM
  • recyclerView ClickEvent
  • gts4mini
  • 코틀린
  • firebase
  • 공돌카돈
  • jetpack
  • 공돌이파파
  • 브레빌 밤비노 플러스
  • IFTTT
  • 프로그래머스
  • Kotlin
  • 라즈베리 파이
  • docker

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
huzit

___을 입력해주세요

Cannot figure out how to save this field into database. You can consider adding a type converter for it.
안드로이드/Debug

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

2023. 6. 12. 09:36
728x90

원인

Room이 필드 타입을 인식하지 못해서 발생하는 문제, 보통 코틀린의 Basic Type이 아닌 직접 만든 데이터 클래스 리스트를 사용할 때 발생

  • Entity 클래스
@Entity(tableName = "checklist")
data class CheckListEntity(
    @ColumnInfo(name = "checklist_info") var checkLists: List<CheckListInfo>
){
    @PrimaryKey(autoGenerate = true)
    var idx: Long = 0
}
  • 데이터 클래스
data class CheckListInfo(
    //할 일
    var checklistContent: String,
    //초기화 요일
    var restartWeek: Set<DayOfWeek>,
    //수행여부
    var done: Boolean = false,
    //애니메이션 visivle
    var visibility: MutableState<Boolean> = mutableStateOf(true)
)

해결

TypeConverter를 추가해주면 된다.

간단히 소개하자면 Room이 인식할 수 없는 타입을 Json으로 변경해서 저장하고 다시 불러올 땐 원래 타입으로 바꿔주는 역할을 한다.

class CheckListConverters {
    @TypeConverter
    fun listToJson(value: List<CheckListInfo>): String?{
        return Gson().toJson(value)
    }

    @TypeConverter
    fun jsonToList(value: String): List<CheckListInfo> {
        val listType = object: TypeToken<List<CheckListInfo>>(){}.type
        return Gson().fromJson(value, listType)

    }
}

jsonToList에서 TypeToken을 쓴 이유는 Json을 원래 타입으로 되돌릴 때 해당하는 타입이 필요하기 때문이다.

Gson().fromJson(value, List<CheckListInfo>)

None of the following functions can be called with the arguments supplied.
지정된 아규먼트만 올 수 있음

안쓰면 오류 뜬다.

728x90
저작자표시 (새창열림)

'안드로이드 > Debug' 카테고리의 다른 글

Unable to create instance of interface androidx.compose.runtime.MutableState. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.  (0) 2023.06.13
Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number.  (0) 2023.06.12
Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules kotlin-stdlib-1.8.10 (org.jetbrains.kotlin:kotlin-stdlib:1.8.10) and kotlin-stdlib-jdk8-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20)  (0) 2023.05.25
java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment  (0) 2023.04.18
RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{dcc7ca6 u0 com.***.***/.ble.BleService}  (0) 2023.04.13
    '안드로이드/Debug' 카테고리의 다른 글
    • Unable to create instance of interface androidx.compose.runtime.MutableState. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.
    • Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number.
    • Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules kotlin-stdlib-1.8.10 (org.jetbrains.kotlin:kotlin-stdlib:1.8.10) and kotlin-stdlib-jdk8-1.7.20 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20)
    • java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
    huzit
    huzit
    simple is best

    티스토리툴바