투케이2K

662. (Android/Java) DataStore 데이터 스토어 사용해 String 형태 key , value 프리퍼런스 데이터 저장 실시 본문

Android

662. (Android/Java) DataStore 데이터 스토어 사용해 String 형태 key , value 프리퍼런스 데이터 저장 실시

투케이2K 2023. 10. 2. 09:19
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

        // ---------------------------------------------------------------
        // [로직 처리 실시]
        // ---------------------------------------------------------------
        try {

            /**
             * ------------------------------------
             * [요약 설명]
             * ------------------------------------
             * 1. DataStore 란 프로토콜 버퍼를 사용하여 키-값 쌍 또는 유형이 지정된 객체를 저장할 수 있는 데이터 저장소 솔루션입니다
             * ------------------------------------
             * 2. DataStore 는 앱 저장소에 데이터를 저장하며, 앱 삭제 또는 앱 데이터 및 캐시 삭제를 하지 않는 경우 데이터가 영구히 보존 됩니다
             * ------------------------------------
             * 3. 설치 라이브러리 :
             *
             *     // [datastore 라이브러리 설치]
             *     implementation "androidx.datastore:datastore-preferences:1.0.0-alpha06"
             *     implementation "androidx.datastore:datastore-preferences-rxjava3:1.0.0-alpha06"
             *
             *     // [Rx 라이브러리 설치]
             *     implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
             *     implementation 'io.reactivex.rxjava3:rxkotlin:3.0.1'
             *     implementation 'io.reactivex.rxjava3:rxjava:3.0.7'
             *
             * ------------------------------------
             * 4. 참고 :
             *
             * https://developer.android.com/topic/libraries/architecture/datastore?hl=ko
             * https://developer.android.com/topic/libraries/architecture/datastore?hl=ko#kotlin
             * ------------------------------------
             * */


            // [DataStore 생성]
            DataStore<Preferences> dataStore = new RxPreferenceDataStoreBuilder(A_Intro.this, "TwokDataStore").build();
            
            
            // [데이터 저장 및 호출 Key 지정]
            Preferences.Key<String> key = new Preferences.Key<>("TWOK_KEY");


            // [DataStore 읽기 : String 타입]
            Flowable<String> readData = RxDataStore.data(dataStore).map(prefs -> prefs.get(key));
            readData.subscribe(
                    value -> {
                        S_Log._W_("DataStore Read Data : " + String.valueOf(value), null);
                    },
                    error -> {
                        S_Log._E_("DataStore Read Error : " + String.valueOf(error.getMessage()), null);
                    },
                    () -> { // [complete]
                    });


            // [DataStore 저장 : String 타입]
            Single<Preferences> updateData =  RxDataStore.updateDataAsync(dataStore,
                    prefsIn -> {
                        MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();

                        // TODO [정의된 key 값에 value 값 저장]
                        mutablePreferences.set(key, "TWOK");

                        return Single.just(mutablePreferences);
                    });


            // [DataStore 저장 : 성공 및 실패 상태 체크]
            updateData.subscribe(
                    preferences -> {
                        S_Log._W_("DataStore Write Data : " + String.valueOf(preferences.get(key)), null);
                    },
                    error -> {
                        S_Log._E_("DataStore Write Error : " + String.valueOf(error.getMessage()), null);
                    });

        }
        catch (Exception e){
            e.printStackTrace();
        }
 

[결과 출력]

 

W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro.lambda$onCreate$1(A_Intro.java:392)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2023-09-27 15:43:13 수요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: DataStore Read Data : TWOK]
W///===========//: ================================================


W///===========//: ================================================
I/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Intro.lambda$onCreate$5(A_Intro.java:416)]
I/: ----------------------------------------------------
I/: [LOG :: NOW TIME :: 2023-09-27 15:43:13 수요일]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: DataStore Write Data : TWOK]
W///===========//: ================================================

 

반응형
Comments