我在My Simple Application中使用了Realm,
我以前版本的Realm 0.86.0 ,我打算将其升级到最新版本 2.1.0
我正在创建RealmProxy来处理领域的所有操作,如此
package devs.lo.fff.utils;
import android.content.Context;
import java.io.Closeable;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.exceptions.RealmException;
public class RealmProxy implements Closeable {
private Realm realm;
private WeakReference<Context> contextWeakReference;
private static ThreadLocal<Set<RealmProxy>> realmsCache = new ThreadLocal<Set<RealmProxy>>() {
@Override
protected Set<RealmProxy> initialValue() {
return new HashSet<>();
}
};
private static final Object mLock = new Object();
public RealmProxy(Context context) {
contextWeakReference = new WeakReference<>(context);
}
public <T extends RealmObject> RealmQuery<T> where(Class<T> clazz) {
checkRealm();
if (!isValid()) {
return null;
}
return realm.where(clazz);
}
public <T extends RealmObject> RealmResults<T> allObjects(Class<T> clazz) {
checkRealm();
if (!isValid()) {
return null;
}
return realm.where(clazz).findAll();
}
public <T extends RealmObject> T createObject(Class<T> clazz) {
if (!isValid()) {
return null;
}
return realm.createObject(clazz);
}
public <T extends RealmObject> T copyToRealm(T object) {
if (!isValid()) {
return null;
}
return realm.copyToRealm(object);
}
public <T extends RealmObject> T copyToRealmOrUpdate(T object) {
if (!isValid()) {
return null;
}
return realm.copyToRealmOrUpdate(object);
}
public <E extends RealmObject> List<E> copyToRealm(Iterable<E> objects) {
if (!isValid()) {
return null;
}
return realm.copyToRealm(objects);
}
public <E extends RealmObject> List<E> copyToRealmOrUpdate(Iterable<E> objects) {
if (!isValid()) {
return null;
}
return realm.copyToRealmOrUpdate(objects);
}
public <E extends RealmObject> void save(final E object) {
executeTransaction(new Transaction() {
@Override
public void execute(RealmProxy realm) {
copyToRealmOrUpdate(object);
}
});
}
public <E extends RealmObject> void save(final Iterable<E> objects) {
executeTransaction(new Transaction() {
@Override
public void execute(RealmProxy realm) {
copyToRealmOrUpdate(objects);
}
});
}
//region experimental
public <T extends RealmObject> RealmProxy set(T object, String fieldName, Object value) {
Method method = findMethod(object, fieldName, value.getClass());
invokeMethod(method, object, value);
return this;
}
public <T extends RealmObject> RealmProxy set(T object, String fieldName, boolean value) {
Method method = findMethod(object, fieldName, boolean.class);
invokeMethod(method, object, value);
return this;
}
public <T extends RealmObject> RealmProxy set(T object, String fieldName, long value) {
Method method = findMethod(object, fieldName, long.class);
invokeMethod(method, object, value);
return this;
}
public <T extends RealmObject> RealmProxy set(T object, String fieldName, float value) {
Method method = findMethod(object, fieldName, float.class);
invokeMethod(method, object, value);
return this;
}
public <T extends RealmObject> RealmProxy set(List<T> objects, String fieldName, Object value) {
if (objects.isEmpty()) {
return this;
}
Method method = findMethod(objects, fieldName, value.getClass());
invokeMethod(method, objects, value);
return this;
}
public <T extends RealmObject> RealmProxy set(List<T> objects, String fieldName, boolean value) {
if (objects.isEmpty()) {
return this;
}
Method method = findMethod(objects, fieldName, boolean.class);
invokeMethod(method, objects, value);
return this;
}
public <T extends RealmObject> RealmProxy set(List<T> objects, String fieldName, long value) {
Method method = findMethod(objects, fieldName, long.class);
invokeMethod(method, objects, value);
return this;
}
public <T extends RealmObject> RealmProxy set(List<T> objects, String fieldName, float value) {
Method method = findMethod(objects, fieldName, float.class);
invokeMethod(method, objects, value);
return this;
}
private <T extends RealmObject> void invokeMethod(Method method, T object, Object value) {
synchronized (mLock) {
internalBeginTransaction(false);
try {
method.invoke(object, value);
internalCommitTransaction();
} catch (IllegalAccessException e) {
e.printStackTrace();
internalCancelTransaction();
} catch (InvocationTargetException e) {
e.printStackTrace();
internalCancelTransaction();
}
}
}
private <T extends RealmObject> void invokeMethod(Method method, List<T> objects, Object value) {
synchronized (mLock) {
internalBeginTransaction(false);
try {
for (int i = 0; i < objects.size(); i++) {
method.invoke(objects.get(i), value);
}
internalCommitTransaction();
} catch (IllegalAccessException e) {
e.printStackTrace();
internalCancelTransaction();
} catch (InvocationTargetException e) {
e.printStackTrace();
internalCancelTransaction();
}
}
}
private synchronized Method findMethod(Object obj,
String property, Class paramType) {
Class<?> theClass = obj.getClass();
String setter = String.format("set%C%s",
property.charAt(0), property.substring(1));
//Class paramType = value.getClass();
try {
return theClass.getMethod(setter, paramType);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private synchronized Method findMethod(List objects,
String property, Class paramType) {
return findMethod(objects.get(0), property, paramType);
}
//endregion
public <T extends RealmObject> void removeFromRealm(T object) {
checkRealm();
synchronized (mLock) {
internalBeginTransaction(false);
try {
if (object.isValid()) {
object.deleteFromRealm();
internalCommitTransaction();
}
} catch (RuntimeException e) {
internalCancelTransaction();
throw new RealmException("Error during transaction.", e);
} catch (Error e) {
internalCancelTransaction();
throw e;
}
}
}
public void beginTransaction() {
beginTransaction(false);
}
public void beginTransaction(boolean commitCurrent) {
synchronized (mLock) {
internalBeginTransaction(commitCurrent);
}
}
private void internalBeginTransaction(boolean commitCurrent) {
checkRealm();
if (realm.isInTransaction() && commitCurrent) {
realm.commitTransaction();
realm.beginTransaction();
} else if (!realm.isInTransaction()) {
realm.beginTransaction();
}
}
public void commitTransaction() {
synchronized (mLock) {
internalCommitTransaction();
}
}
private void internalCommitTransaction() {
//checkRealm();
if (isValid() && realm.isInTransaction()) {
realm.commitTransaction();
}
}
public void cancelTransaction() {
synchronized (mLock) {
//checkRealm();
internalCancelTransaction();
}
}
private void internalCancelTransaction() {
//checkRealm();
if (isValid() && realm.isInTransaction()) {
realm.cancelTransaction();
}
}
public void clear(Class<? extends RealmObject> classSpec, boolean autoTransaction) {
if (autoTransaction) {
beginTransaction();
realm.delete(classSpec);
commitTransaction();
} else {
realm.delete(classSpec);
}
}
@Override
public void close() {
internalClose();
realmsCache.get().remove(this);
}
private void internalClose() {
if (realm != null) {
realm.close();
realm = null;
}
}
private void checkRealm() {
if (!isValid()) {
Context context = contextWeakReference.get();
if (context == null) {
throw new IllegalStateException("Context is null, Activity or Fragment is already killed");
}
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name(Realm.DEFAULT_REALM_NAME)
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(config);
realmsCache.get().add(this);
}
}
public boolean isValid() {
return !(realm == null || realm.isClosed());
}
public <T extends RealmObject> void executeTransaction(T object, AdvancedTransaction<T> transaction) {
checkRealm();
synchronized (mLock) {
internalBeginTransaction(false);
try {
if (object.isValid()) {
transaction.execute(object);
internalCommitTransaction();
}
} catch (RuntimeException e) {
internalCancelTransaction();
throw new RealmException("Error during transaction.", e);
} catch (Error e) {
internalCancelTransaction();
throw e;
}
}
}
public void executeTransaction(Transaction transaction) {
executeTransaction(transaction, false, false, null);
}
public void executeTransaction(Transaction transaction, boolean commitCurrent) {
executeTransaction(transaction, commitCurrent, false, null);
}
public void executeTransaction(Transaction transaction, boolean commitCurrent, boolean doInBackground) {
executeTransaction(transaction, commitCurrent, doInBackground, null);
}
public synchronized void executeTransaction(final Transaction transaction, boolean commitCurrent, boolean doInBackground, Realm.Transaction.Callback callbacks) {
if (transaction == null || !isValid()) {
return;
}
checkRealm();
if (doInBackground) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(final Realm realm) {
transaction.execute(RealmProxy.this);
}
}, callbacks);
} else {
synchronized (mLock) {
internalBeginTransaction(commitCurrent);
try {
transaction.execute(this);
internalCommitTransaction();
} catch (RuntimeException e) {
internalCancelTransaction();
throw new RealmException("Error during transaction.", e);
} catch (Error e) {
internalCancelTransaction();
throw e;
}
}
}
}
public interface Transaction {
void execute(RealmProxy realm);
}
public interface AdvancedTransaction<T extends RealmObject> {
void execute(T object);
}
public void clearDataBase() {
Context context = contextWeakReference.get();
if (context == null) {
return;
}
RealmConfiguration config;
if (realm == null) {
config = new RealmConfiguration.Builder(context)
.name(Realm.DEFAULT_REALM_NAME)
.deleteRealmIfMigrationNeeded()
.build();
} else {
config = realm.getConfiguration();
}
/** all realm instances must be closed before deleting the file */
for (RealmProxy realmProxy : realmsCache.get()) {
realmProxy.internalClose();
}
/** clear threadLocal, its no longer needed*/
realmsCache.remove();
Realm.deleteRealm(config);
/*File appDir = context.getExternalFilesDir("Documents");
File[] files;
if (appDir != null) {
files = appDir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
file.delete();
}
}
appDir.delete();
}*/
}
public static void clearDataBase(final Context context) {
RealmProxy realm = new RealmProxy(context);
realm.clearDataBase();
realm.close();
}
}
但升级到2.1.0之后这个文件出现了一些错误,我正试图解决它,但似乎有一些错误,我不知道如何解决它
请建议
提前致谢。
答案 0 :(得分:0)
https://github.com/realm/realm-java/blob/master/CHANGELOG.md
2.0.0
重大改变
现在需要在调用任何其他Realm API之前调用
Realm.init(Context)
。
Removed RealmConfiguration.Builder(Context)
,RealmConfiguration.Builder(Context, File)
和RealmConfiguration.Builder(File)
构造函数。0.89.0
突破变化
RealmChangeListener
也提供了更改的对象/ Realm /集合(#1594)。0.88.0
已过时
Realm.executeTransaction(Transaction, Callback)
,并将其替换为
Realm.executeTransactionAsync(Transaction)
,
Realm.executeTransactionAsync(Transaction, OnSuccess)
,
Realm.executeTransactionAsync(Transaction, OnError)
Realm.executeTransactionAsync(Transaction, OnSuccess, OnError)
。