rxjava

BasicRxJavaDemo
數(shù)據(jù)庫設計:

字段名 說明
userid 用戶 id(primarykey)
username 用戶名字

1.Entity

@Entity(tableName = "users")
public class UserEntity {

    @PrimaryKey
    @ColumnInfo(name = "userid")
    private String mId;

    @ColumnInfo(name = "username")
    private String mUserName;

    @Ignore
    public UserEntity(String userName) {
        mId = UUID.randomUUID().toString();
        mUserName = userName;
    }

    public UserEntity(String id, String userName) {
        this.mId = id;
        this.mUserName = userName;
    }

    public String getId() {
        return mId;
    }

    public String getUserName() {
        return mUserName;
    }
}

2.dao

@Dao
public interface UserDao {

    /**
     * Get the user from the table. Since, for simplicity we only have one user in the database,
     * this query gets all users from the table, but limits the result to just the 1st user.
     *
     * @return the user from the table
     */
    @Query("SELECT * FROM Users LIMIT 1")
    Flowable<UserEntity> getUser();

    /**
     * Insert a user in the database. If the user already exists, replace it.
     *
     * @param user the user to be inserted.
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertUser(UserEntity user);

    /**
     * Delete all users.
     */
    @Query("DELETE FROM Users")
    void deleteAllUsers();
}

3.viewModel

public class UserViewModel extends ViewModel {

    private final UserDataSource mDataSource;

    private UserEntity mUser;

    public UserViewModel(UserDataSource dataSource) {
        mDataSource = dataSource;
    }

    /**
     * Get the user name of the user.
     *
     * @return a {@link Flowable} that will emit every time the user name has been updated.
     */
    public Flowable<String> getUserName() {
        return mDataSource.getUser()
                // for every emission of the user, get the user name
                .map(new Function<UserEntity, String>() {
                    @Override
                    public String apply(UserEntity user) throws Exception {
                        return user.getUserName();
                    }
                });

    }

    /**
     * Update the user name.
     *
     * @param userName the new user name
     * @return a {@link Completable} that completes when the user name is updated
     */
    public Completable updateUserName(final String userName) {

        return new CompletableFromAction(new Action() {
            @Override
            public void run() throws Exception {
                // if there's no use, create a new user.
                // if we already have a user, then, since the user object is immutable,
                // create a new user, with the id of the previous user and the updated user name.
                mUser = mUser == null
                        ? new UserEntity(userName)
                        : new UserEntity(mUser.getId(), userName);

                mDataSource.insertOrUpdateUser(mUser);
            }
        });
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末蓝翰,一起剝皮案震驚了整個濱河市绽族,隨后出現(xiàn)的幾起案子厌蔽,更是在濱河造成了極大的恐慌氯窍,老刑警劉巖忍坷,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件穆桂,死亡現(xiàn)場離奇詭異,居然都是意外死亡盏浙,警方通過查閱死者的電腦和手機眉睹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來废膘,“玉大人辣往,你說我怎么就攤上這事≈潮埃” “怎么了站削?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長孵稽。 經常有香客問我许起,道長,這世上最難降的妖魔是什么菩鲜? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任园细,我火速辦了婚禮,結果婚禮上接校,老公的妹妹穿的比我還像新娘猛频。我一直安慰自己狮崩,他們只是感情好,可當我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布鹿寻。 她就那樣靜靜地躺著睦柴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪毡熏。 梳的紋絲不亂的頭發(fā)上坦敌,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天,我揣著相機與錄音痢法,去河邊找鬼狱窘。 笑死,一個胖子當著我的面吹牛财搁,可吹牛的內容都是我干的蘸炸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼尖奔,長吁一口氣:“原來是場噩夢啊……” “哼幻馁!你這毒婦竟也來了?” 一聲冷哼從身側響起越锈,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎膘滨,沒想到半個月后甘凭,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡火邓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年丹弱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铲咨。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡躲胳,死狀恐怖,靈堂內的尸體忽然破棺而出纤勒,到底是詐尸還是另有隱情坯苹,我是刑警寧澤,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布摇天,位于F島的核電站粹湃,受9級特大地震影響,放射性物質發(fā)生泄漏泉坐。R本人自食惡果不足惜为鳄,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望腕让。 院中可真熱鬧孤钦,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至壳猜,卻和暖如春勾徽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背统扳。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工喘帚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咒钟。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓吹由,卻偏偏與公主長得像,于是被迫代替她去往敵國和親朱嘴。 傳聞我的和親對象是個殘疾皇子倾鲫,可洞房花燭夜當晚...
    茶點故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內容