Last active
October 27, 2021 15:30
-
-
Save vorobeij/c5cf4a790b2eccbf5cea568e9818f605 to your computer and use it in GitHub Desktop.
room android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Entity(tableName = "rss_feeds") | |
| class DataHolder(@PrimaryKey(autoGenerate = false) | |
| @ColumnInfo(name = "link") | |
| var link: String, | |
| @ColumnInfo(name = "title") | |
| var title: String, | |
| @ColumnInfo(name = "date") | |
| var date: Long, | |
| @ColumnInfo(name = "img_url") | |
| var imgUrl: String = "", | |
| @ColumnInfo(name = "bookmarked") | |
| var bookmarked: Boolean = false, | |
| @ColumnInfo(name = "was_read") | |
| var wasRead: Boolean = false) | |
| @Dao | |
| interface RssItemsDao { | |
| @Insert(onConflict = OnConflictStrategy.IGNORE) | |
| fun saveAll(list: List<DataHolder>) | |
| @Query("SELECT * from rss_feeds WHERE link = :link LIMIT 1") | |
| fun get(link:String): DataHolder | |
| @Query("SELECT * from rss_feeds") | |
| fun getAll(): List<DataHolder> | |
| @Query("SELECT * from rss_feeds WHERE bookmarked='true'") | |
| fun getBookmarked(): List<DataHolder> | |
| @Update | |
| fun updateRssItems(rssItems: List<DataHolder>):Int | |
| @Update | |
| fun updateRssItem(rssItem: DataHolder):Int | |
| @Query("DELETE from rss_feeds") | |
| fun deleteAll() | |
| } | |
| @Database(entities = [(DataHolder::class)], version = 1) | |
| abstract class RssDataBase : RoomDatabase() { | |
| abstract fun rssDataDao(): RssItemsDao | |
| companion object { | |
| private var INSTANCE: RssDataBase? = null | |
| fun getInstance(context: Context): RssDataBase { | |
| if (INSTANCE == null) { | |
| synchronized(RssDataBase::class) { | |
| INSTANCE = Room.databaseBuilder(context.applicationContext, | |
| RssDataBase::class.java, | |
| "rss.db").build() | |
| } | |
| } | |
| return INSTANCE!! | |
| } | |
| fun destroyInstance() { | |
| INSTANCE = null | |
| } | |
| } | |
| } | |
| // Usage: | |
| RssDataBase.getInstance(context).rssDataDao().<method_name> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies { | |
| def room_version = "1.1.0" // or, for latest rc, use "1.1.1-rc1" | |
| implementation "android.arch.persistence.room:runtime:$room_version" | |
| annotationProcessor "android.arch.persistence.room:compiler:$room_version" | |
| // optional - RxJava support for Room | |
| implementation "android.arch.persistence.room:rxjava2:$room_version" | |
| // optional - Guava support for Room, including Optional and ListenableFuture | |
| implementation "android.arch.persistence.room:guava:$room_version" | |
| // Test helpers | |
| testImplementation "android.arch.persistence.room:testing:$room_version" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment