1.AsyncTask抽象類
功能:
AsyncTask 屬于安卓中實(shí)現(xiàn)多線程的一種方法淑履,可以在該線程中實(shí)施耗時(shí)方法隶垮,比如數(shù)據(jù)庫查詢。也可以實(shí)現(xiàn)工作線程和主線程之間的通信秘噪。
類別:
屬于抽象(abstract)類狸吞,使用時(shí)需extends。
參數(shù):
含有三個(gè)泛型參數(shù):
public abstract class AsyncTask<Params, Progress, Result> { ... }
// 類中參數(shù)為3種泛型類型
// 整體作用:控制AsyncTask子類執(zhí)行線程任務(wù)時(shí)各個(gè)階段的返回類型
// 具體說明:
// a. Params:開始異步任務(wù)執(zhí)行時(shí)傳入的參數(shù)類型指煎,對(duì)應(yīng)excute()中傳遞的參數(shù)
// b. Progress:異步任務(wù)執(zhí)行過程中蹋偏,返回下載進(jìn)度值的類型
// c. Result:異步任務(wù)執(zhí)行完成后,返回的結(jié)果類型至壤,與doInBackground()的返回值類型保持一致
// 注:
// a. 使用時(shí)并不是所有類型都被使用
// b. 若無被使用威始,可用java.lang.Void類型代替
// c. 若有不同業(yè)務(wù),需額外再寫1個(gè)AsyncTask的子類}
2.Dao
interactions 交互? ?compile 編譯
以下是官網(wǎng)的描述:
Marks the class as a Data Access Object.
Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods.
數(shù)據(jù)接入對(duì)象是一個(gè)你定義你與你的數(shù)據(jù)庫交互的主類像街,他們中有各種各樣的查詢方法黎棠。
The class marked with?@Dao?should either be an interface or an abstract class. At compile time, Room will generate an implementation of this class when it is referenced by a?Database.
這個(gè)類應(yīng)當(dāng)被@Dao標(biāo)記,并且應(yīng)該是一個(gè)接口或者抽象類镰绎。在編譯的時(shí)候脓斩,Room會(huì)產(chǎn)生該類的實(shí)現(xiàn),當(dāng)它被數(shù)據(jù)庫引用的時(shí)候畴栖。
An abstract?@Dao?class can optionally have a constructor that takes a?Database?as its only parameter.
一個(gè)@Dao的抽象類能選擇性的有一個(gè)把數(shù)據(jù)庫當(dāng)作其唯一參數(shù)的構(gòu)造方法随静。
It is recommended to have multiple?Dao?classes in your codebase depending on the tables they touch.
根據(jù)他們關(guān)聯(lián)的表格,建議在你的代碼庫中有個(gè)復(fù)合的Dao類吗讶。
查詢操作很重要燎猛,可以結(jié)合SQL語句實(shí)現(xiàn)非常有針對(duì)性的查詢:
3.Database
alter?更改;修改? ?mandatory 強(qiáng)制的
Marks a class as a RoomDatabase.
標(biāo)記為RoomDatabase類
The class should be an abstract class and extend?RoomDatabase.
這個(gè)類應(yīng)該為一個(gè)繼承RoomDatabase的抽象類
You can receive an implementation of the class via?Room.databaseBuilder?or?Room.inMemoryDatabaseBuilder.
你能通過Room.databaseBuilder?or?Room.inMemoryDatabaseBuilder.這兩種方式接受到一個(gè)本類的實(shí)現(xiàn)关翎。
// Song and Album are classes annotated with @Entity.
@Database(version = 1, entities = {Song.class, Album.class})
abstract class MusicDatabase extends RoomDatabase {
// SongDao is a class annotated with @Dao.
abstract public SongDao getSongDao();
// AlbumDao is a class annotated with @Dao.
abstract public ArtistDao getArtistDao();
// SongAlbumDao is a class annotated with @Dao.
abstract public SongAlbumDao getSongAlbumDao(); }
The example above defines a class that has 2 tables and 3 DAO classes that are used to access it.?
在上面的引用中扛门,定義了一個(gè)數(shù)據(jù)庫類,其中有2個(gè)表格和3個(gè)Dao類去接入它纵寝。
There is no limit on the number of?Entity?or?Dao?classes but they must be unique within the Database.
實(shí)體和Dao在數(shù)據(jù)庫中的數(shù)目是沒有限制的论寨,但是他們?cè)谶@個(gè)數(shù)據(jù)庫中必須是唯一的星立。
Instead of running queries on the database directly, you are highly recommended to create?Dao?classes. Using Dao classes will allow you to abstract the database communication in a more logical layer which will be much easier to mock in tests (compared to running direct SQL queries). It also automatically does the conversion from?Cursor?to your application data classes so you don't need to deal with lower level database APIs for most of your data access.
上面這段話的大概意思是,除非你直接對(duì)數(shù)據(jù)庫使用查詢操作葬凳,否則绰垂,強(qiáng)烈建議你在數(shù)據(jù)庫類中創(chuàng)建一個(gè)Dao類,這能大幅度提升你的效率火焰,從而避免了直接去使用復(fù)雜的操作數(shù)據(jù)庫的語言劲装。
Room also verifies all of your queries in?Dao?classes while the application is being compiled so that if there is a problem in one of the queries, you will be notified instantly.
Room在應(yīng)用編譯的時(shí)候會(huì)在Dao中核驗(yàn)?zāi)闼械牟樵儾僮鳎@樣昌简,一旦有問題出現(xiàn)占业,你能夠馬上察覺到。
其參數(shù)纯赎,即@Database后面括號(hào)呢的參數(shù):
entities谦疾、version、exportSchema犬金、views
4.Entiy
重點(diǎn):
1念恍、一個(gè)Entity對(duì)象代表數(shù)據(jù)表中的一行,一個(gè)Entity類代表一張數(shù)據(jù)表晚顷。
2峰伙、Entity中的成員變量都是數(shù)據(jù)表中的列。
3该默、一個(gè)Java類定義成Entity只要加上Entity注解就可以了瞳氓。
主鍵Primary Key:每一個(gè)Entity至少定義一個(gè)主鍵(primary key),哪怕Entity中只有一個(gè)變量也要將這個(gè)變量定義為主鍵权均,在Room數(shù)據(jù)庫中使用注解 @PrimaryKey 來定義主鍵顿膨,@PrimaryKey 的使用方式有兩種一種是在類變量前面加,如果主鍵比較復(fù)雜可以加在@Entity注解的后面叽赊。
列名:和改變表名稱tableName一樣恋沃,可以改變表中的列名稱,使用 @ColumnInfo來改變列的名稱必指。如果不改的話默認(rèn)使用變量名的小寫形式囊咏。(注意:前面說了,Entity類中所有的變量都是數(shù)據(jù)表中的列塔橡。
使某些變量不生成數(shù)據(jù)表中的字段:由上面可知當(dāng)一個(gè)類前加了Entity注解后類中的所有成員變量都會(huì)生成表中的屬性列梅割,如果我們不希望某個(gè)變量生成表中的屬性列,可以使用注解 @Ignore葛家。
嵌套Entity:
如果定義的Entity類里面有個(gè)實(shí)體對(duì)象户辞,并且希望定義的Entity中的表列字段包含該Entity類對(duì)象中的變量,可以在Entity類對(duì)象中加@Embedded標(biāo)注癞谒。
這樣就可以實(shí)現(xiàn)實(shí)體之間的組合底燎。
application從RoomDatabase中獲取DAO實(shí)例刃榨,并且通過DAO中定義的方法來操作數(shù)據(jù)庫中Entity。獲取設(shè)置entity的數(shù)據(jù)双仍。