欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用ContentProvider,多線程app取到值不一致

在安卓中四大組件之一ContentProvider進程就不一致,于是我就改自己這邊邏輯,也取和改數(shù)據(jù)通過我的大家一定不陌生,我最近在優(yōu)化自己的模塊時發(fā)生一個BUG,我是提供Provider數(shù)據(jù)的app,但是其他來取我數(shù)據(jù)的app和我保存的值不一樣。

成都創(chuàng)新互聯(lián)公司長期為近千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為坡頭企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站設(shè)計,坡頭網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

后來發(fā)現(xiàn)因為我是用SharedPreferences+ContentProvider的方式存儲數(shù)據(jù)提供給各個APP的,原來我的主程序代碼和Provider在一個進程下,但調(diào)我Provider的APP太多了,導(dǎo)致我的程序即使退出在后臺也占用內(nèi)存不會殺死,于是把Provider獨立了另一個進程,于是主進程就能釋放內(nèi)存了。但是引起了以上問題。

經(jīng)過查了很多文章,推敲應(yīng)該是由于SharedPreferences在多線程下的確會取得數(shù)據(jù)不一致,看到一個大佬的回答有所啟發(fā)。我以前取數(shù)據(jù)都是直接從自己目錄下SharedPreferences里拿的,那進程就是我主程序的進程,和提供其他APP數(shù)據(jù)的Provider進程就不一致,于是我就改自己這邊邏輯,也取和改數(shù)據(jù)通過我的Provider來操作,終于解決了這個問題。

public static UserInfoBean getLoginedUserInfo () {

    UserInfoBean userInfo = null;

    ContentResolver cr = UcApp.sCtx.getContentResolver();
    Cursor cursor = cr.query(Const.URI.USER_INFO,
            null, null, null, null);

    if ( null == cursor ) {
        LogUtils.w("getLoginedUserInfo cursor == null");
        return userInfo;
    }

    Bundle bundle = cursor.getExtras();

    boolean isLogined = bundle.getBoolean(Const.SpUser.Key.IS_LOGINED, false);
    if ( ! isLogined ) {
        LogUtils.w("getLoginedUserInfo isLogined = false");
        return userInfo;
    }

    userInfo = new UserInfoBean();
    userInfo.isLogined = true;
    userInfo.username = bundle.getString(Const.SpUser.Key.USERNAME, "");
    userInfo.token = bundle.getString(Const.SpUser.Key.TOKEN, "");

    cursor.close();

    LogUtils.w(userInfo.toString());

    return userInfo;
}

        public static void saveLoginedUserInfo (UserInfoBean userInfo, boolean isSaveToken) {

    if ( null == userInfo ) {
        return;
    }

    ContentResolver cr = UcApp.sCtx.getContentResolver();
    ContentValues values = new ContentValues();

    values.put(Const.SpUser.Key.IS_LOGINED, true);
    values.put(Const.SpUser.Key.USERNAME, userInfo.username);
    values.put(Const.SpUser.Key.TOKEN, userInfo.token);

    userInfo.usernameEncode = ComUtils.URLEncoder(userInfo.username);
    values.put(Const.SpUser.Key.USERNAME_ENCODE, userInfo.usernameEncode);

    cr.update(Const.URI.USER_INFO, values, null, null);
}

        @Override
public Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs,
                     String sortOrder) {

    Cursor cursor = new MatrixCursor(new String[]{});
    Bundle bundle = new Bundle();

        UserInfoBean userInfo = getLoginedUserInfo();

        if ( null != userInfo ) {
            bundle.putBoolean(Const.SpUser.Key.IS_LOGINED, true);
            bundle.putString(Const.SpUser.Key.USERNAME, userInfo.username);
            bundle.putString(Const.SpUser.Key.TOKEN, userInfo.token);
        } else {
            bundle.putBoolean(Const.SpUser.Key.IS_LOGINED, false);
        }
    }

    LogUtils.w(bundle.toString());

    return cursor;
}

@Override
public int update (Uri uri, ContentValues values, String selection,
                   String[] selectionArgs) {

    if ( null == values ) {
        return 0;
    }

    LogUtils.w(values.toString());

    SpUtils spUtils = new SpUtils(Const.SpUser.NAME);
    Set<String> keys = values.keySet();
    for ( String key : keys ) {
        Object obj = values.get(key);
        if ( null != obj ) {
            spUtils.add(key, obj);
        }
    }
    spUtils.apply();

    notifyChangeUserInfo();

    return 1;
}

private UserInfoBean getLoginedUserInfo () {

    UserInfoBean userInfo = null;
    SpUtils spUtils = new SpUtils(Const.SpUser.NAME);

    boolean isLogined = (boolean) spUtils.get(Const.SpUser.Key.IS_LOGINED, false);
    if ( ! isLogined ) {
        LogUtils.w("isLogined = false");
        return userInfo;
    }

    userInfo = new UserInfoBean();
    userInfo.isLogined = true;
    userInfo.username = (String) spUtils.get(Const.SpUser.Key.USERNAME, "");
    userInfo.token = (String) spUtils.get(Const.SpUser.Key.TOKEN, "");

    return userInfo;
}

網(wǎng)站題目:使用ContentProvider,多線程app取到值不一致
URL地址:http://chinadenli.net/article10/ihecdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、定制開發(fā)、外貿(mào)建站、面包屑導(dǎo)航網(wǎng)站設(shè)計網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化