2017/02/23
追加情報
#マイグレーション処理
import android.util.Log;
import io.realm.DynamicRealm;
import io.realm.RealmMigration;
import io.realm.RealmSchema;
public class Migration implements RealmMigration {
private static final String TAG = Migration.class.getName();
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
Log.d(TAG, "realm migration version:" + String.valueOf(oldVersion));
RealmSchema schema = realm.getSchema();
}
}
Realm設定部分
private static RealmConfiguration mDefaultConfig;
private static RealmConfiguration mReadOnlyConfig;
@RealmModule(classes = {
ChildArticleEntity.class,
ChildEntity.class,
ChildNotificationEntity.class,
FavoriteArticleEntity.class,
ReadArticleEntity.class
})
public static class DefaultModule {
}
@RealmModule(classes = {
ArticleEntity.class,
CategoryEntity.class,
DateEventEntity.class,
DaysOldEventEntity.class,
DaysOldNotificationEntity.class,
MonthsOldEventEntity.class,
MonthsOldNotificationEntity.class,
SuggestSearchWordEntity.class,
TimelineEntity.class
})
public static class ReadOnlyModule {
}
public static void setupDefaultRealm() {
if (mDefaultConfig == null) {
mDefaultConfig = new RealmConfiguration.Builder()
.schemaVersion(CURRENT_SCHEME_VERSION)
.migration(new Migration())
.modules(new DefaultModule())
.build();
}
Realm.setDefaultConfiguration(mDefaultConfig);
}
public static Realm getReadonlyInstance() {
if (mReadOnlyConfig == null) {
mReadOnlyConfig = new RealmConfiguration.Builder()
.name(READ_ONLY_FILE_NAME)
.schemaVersion(CURRENT_SCHEME_VERSION)
.migration(new Migration())
.modules(new ReadOnlyModule())
.build();
}
return Realm.getInstance(mReadOnlyConfig);
}
スキーマ
import java.util.Date;
import io.realm.RealmObject;
public class ChildNotificationEntity extends RealmObject {
private int childId;
private String message;
private String notifyAt;
public int getChildId() {
return childId;
}
public void setChildId(int childId) {
this.childId = childId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getNotifyAt() {
return notifyAt;
}
public void setNotifyAt(String notifyAt) {
this.notifyAt = notifyAt;
}
}
あらかじめ作成したrealmファイルをアプリケーションに読み込む処理
public static void importReadonlyRealmIfNeeded(Context context) {
try {
//端末内保持バージョン
SharedPreferences s = context.getSharedPreferences(AppConst.PACKAGE_NAME, Context.MODE_PRIVATE);
int versionCode = s.getInt(KEY_VERSION_CODE, 0);
//現在のビルドバージョン
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
int currentVersionCode = pInfo.versionCode;
//version codeがアップデートされるとインポートが走る
if (versionCode == currentVersionCode) {
Log.i("realm", "import skip");
return;
}
Log.d(TAG, "Copy readonly.realm to this device");
copyBundledRealmFile(context.getResources().openRawResource(R.raw.readonly), READ_ONLY_FILE_NAME, context);
s.edit().putInt(KEY_VERSION_CODE, currentVersionCode).apply();
Log.i("realm", "success import");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
private static void copyBundledRealmFile(InputStream inputStream, String outFileName, Context context) {
try {
File file = new File(context.getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}