Android 앱에 SharedPreferences
을 처음 사용했습니다. SharedPreferences
을 계속해서 사용할 것이므로 SharedPreferencesUtil
이라는 유틸리티 클래스를 만들었습니다.이 유틸리티 클래스에는 값에 액세스하고 수정할 수있는 많은 정적 메서드가 포함되어 있습니다. 예를 들어 :SharedPreferencesUtil 만들기
/**
* This method is used to add an event that the user
* is looking forward to. We use the objectId of a ParseObject
* because every object has a unique ID which helps to identify the
* event.
* @param objectId The id of the ParseObject that represents the event
*/
public static void addEventId(String objectId){
assert context != null;
prefs = context.getSharedPreferences(Fields.SHARED_PREFS_FILE, 0);
// Get a reference to the already existing set of objectIds (events)
Set<String> myEvents = prefs.getStringSet(Fields.MY_EVENTS, new HashSet<String>());
myEvents.add(objectId);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(Fields.MY_EVENTS, myEvents);
editor.commit();
}
나는 질문의 몇 가지가 있습니다
1.이 유틸리티 클래스 SharedPreferencesUtil
을 가지고 좋은 결정인가?
2. assert
의 사용이 적절합니까?
3. String
을 어떻게 추가 할 예정입니까?
포인트 1은 내가 현재하고있는 것입니다. 포인트 2는 내가하고 싶은 일이지만 어설 션은 더 단순 해 보였습니다. –
어설 션은 기본적으로 활성화되어 있지 않으므로 앱을 릴리스 할 때 그대로두면 안됩니다. 게다가 적절한 해결 방법 (call initialize())을 명시하는 특정 예외를 던지는 것이 좋습니다. – kcoppock