Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for type adapters#1592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
clementetb wants to merge32 commits intomain
base:main
Choose a base branch
Loading
fromct/type-adapters
Draft

Conversation

@clementetb
Copy link
Contributor

@clementetbclementetb commentedDec 5, 2023
edited
Loading

Type adapters allow users to persist their types in Realm by doing some transformation to Realm-supported types.

Use cases

Compile time adapter

Adapters defined as objects will be globally available on all Realm instances.

object RealmInstantDateAdapter: RealmTypeAdapter<RealmInstant, Date> {overridefunfromRealm(realmValue:RealmInstant):Date=TODO()overridefuntoRealm(value:Date):RealmInstant=TODO()}classMyObject:RealmObject {    @TypeAdapter(RealmInstantDateAdapter::class)var date:Date=Date()}

Runtime adapter

Adapters defined as classes require an adapter instance to be passed to the Realm instance.

classEncryptedStringAdapter(valencryptionKey:String,) : RealmTypeAdapter<ByteArray, String> {overridefunfromRealm(realmValue:ByteArray):String=TODO()overridefuntoRealm(value:String):ByteArray=TODO()}classMyObject :RealmObject {    @TypeAdapter(EncryptedStringAdapter::class)var secureString:String="some content"}funcreateRealmConfig()=RealmConfiguration        .Builder(setOf(MyObject::class))        .typeAdapters {            add(EncryptedStringAdapter("my encryption key"))        }        .build()

Use adapters in Realm collection type parameters.

classMyObject :RealmObject {var secureStringList:RealmList<@TypeAdapter(EncryptedStringAdapter::class)String>= realmListOf()}

Type alias compatibility

typealiasEncryptedString= @TypeAdapter(EncryptedStringAdapter::class)StringclassMyObject :RealmObject {var secureString:EncryptedString=""var secureStringList:RealmList<EncryptedString>= realmListOf()}

Compatibility with other annotations

object StringDateAdapter: RealmTypeAdapter<String, Date> {overridefunfromRealm(realmValue:String):Date=TODO()overridefuntoRealm(value:Date):String=TODO()}classMyObject :RealmObject {    @PrimaryKey    @TypeAdapter(StringDateAdapter::class)var primaryKey:Date=Date()        @Index    @TypeAdapter(StringDateAdapter::class)var indexed:Date=Date()    @PersistedName    @TypeAdapter(StringDateAdapter::class)var persistedName:Date=Date()    @FullText    @TypeAdapter(StringDateAdapter::class)var date:Date=Date()}

Not in this PR

Apply adapters to a whole class/file

@file:UseTypeAdapters(adapters= [EncryptedStringAdapter::class])// or@UseTypeAdapters(adapters= [EncryptedStringAdapter::class])classMyObject :RealmObject {var secureString:String="some content"}

Automatic enum support (#829)

enumclassNumbers {ONE,TWO,THREE,}classMyObject :RealmObject {var enumValue:Numbers=ONE}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

@clementetbclementetb

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@clementetb

[8]ページ先頭

©2009-2025 Movatter.jp