gpt4 book ai didi

java - 关闭 Realm 的所有本地实例

转载 作者:行者123 更新时间:2023-11-29 19:15:59 27 4
gpt4 key购买 nike

我有一个问题,关于 Realm 的关闭实例。我使用 ThreadPoolExecutor 进行多线程处理,每个线程都使用自己的 Realm 实例。我在每次使用 Realm 之前调用 Realm.getDefaultInstance()。每次我调用 Realm 时,我都会获取旧数据,因为每次我都从不同的线程调用 Realm。

我查看了 Realm 的来源,看到了 33 个 Realm 的本地引用。当我调用 Realm.getDefaultInstance().close() 然后检查对象是否已关闭时,我得到了 false。但现在我有 32 个引用文献。我如何关闭 Realm 的所有实例,重置内存缓存并获取实际数据?

最佳答案

我用 RealmManager 解决了我的问题,安装了 Realm.getDefaultInstance(),我使用 RealmManager.getInstance()。在每次 Realm.getDefaultInstance()

之后都不需要调用 realm.close()

Realm 管理器:

import android.util.LongSparseArray;

import io.realm.Realm;

public class RealmManager {
private volatile static LongSparseArray<Realm> THREAD_INSTANCES = new LongSparseArray<>();

public static Realm getInstance() {
final long threadId = Thread.currentThread().getId();

if (THREAD_INSTANCES.indexOfKey(threadId) >= 0) {
Realm instance = THREAD_INSTANCES.get(threadId);

if (instance == null || instance.isClosed()) {
instance = Realm.getDefaultInstance();

THREAD_INSTANCES.put(threadId, instance);
}

return instance;
} else {
Realm instance = Realm.getDefaultInstance();
THREAD_INSTANCES.put(threadId, instance);

return instance;
}
}

public static void closeInstance() {
long threadId = Thread.currentThread().getId();

if (THREAD_INSTANCES.indexOfKey(threadId) >= 0) {
Realm instance = THREAD_INSTANCES.get(threadId);

if (instance == null || instance.isClosed()) {
THREAD_INSTANCES.remove(threadId);
} else {
instance.close();
instance = null;

THREAD_INSTANCES.remove(threadId);
}
}
}
}

我的作业执行器:

public class JobExecutor implements Executor {
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
private final java.util.concurrent.ExecutorService mThreadPoolExecutor;
private final DiskDataSource mDiskDataSource;

public JobExecutor(DiskDataSource diskDataSource) {
mDiskDataSource = diskDataSource;
mThreadPoolExecutor = Executors.newFixedThreadPool(CORE_POOL_SIZE, new JobThreadFactory());
}

@Override
public void execute(@NonNull Runnable runnable) {
mThreadPoolExecutor.execute(new RealmRunnable(mDiskDataSource, runnable));
}

private static class JobThreadFactory implements ThreadFactory {
private static final String THREAD_NAME = "android_";
private int counter = 0;
@Override
public Thread newThread(@NonNull Runnable runnable) {
return new Thread(runnable, THREAD_NAME + counter++);
}
}
}

RealmRunnable:

public class RealmRunnable implements Runnable {
private Runnable mOrigRunnable;
private DiskDataSource mDiskDataSource;

public RealmRunnable(DiskDataSource diskDataSource, Runnable origRunnable) {
mDiskDataSource = diskDataSource;
mOrigRunnable = origRunnable;
}

@Override
public void run() {
try {
mOrigRunnable.run();
} finally {
mDiskDataSource.closeDatabaseInstance();
}
}
}

DiskDataSource 中,我使用 RealmManager

关于java - 关闭 Realm 的所有本地实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43490892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com