无法在以下路径打开领域:open()失败:打开的文件太多

时间:2019-12-18 13:00:21

标签: android multithreading android-studio realm android-handlerthread

我有一个执行许多领域操作的android应用程序。我试图使用处理程序线程来加快数据库操作,但是这次我遇到了这些错误。我已经审查了类似的问题,但找不到原因。

12-17 20:07:30.826  6387  8777 W System.err: io.realm.exceptions.RealmFileException: Unable to open a realm at path '/data/data/br.com.gomus.androidapp/files/default.realm': open() failed: Too many open files. (open("/data/data/br.com.gomus.androidapp/files/default.realm") failed: Too many open files) (/data/data/br.com.gomus.androidapp/files/default.realm) in /Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 101 Kind: ACCESS_ERROR.
12-17 20:07:30.827  6387  8777 W System.err:    at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
12-17 20:07:30.827  6387  8777 W System.err:    at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:184)
12-17 20:07:30.827  6387  8777 W System.err:    at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:254)
12-17 20:07:30.828  6387  8777 W System.err:    at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:244)
12-17 20:07:30.828  6387  8777 W System.err:    at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:319)
12-17 20:07:30.828  6387  8777 W System.err:    at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
12-17 20:07:30.828  6387  8777 W System.err:    at io.realm.Realm.getDefaultInstance(Realm.java:332)
12-17 20:07:30.828  6387  8777 W System.err:    at br.com.gomus.androidapp.data.local.database.LocalDatabaseImpl.findRealmObject(LocalDatabaseImpl.java:284)
12-17 20:07:30.829  6387  8777 W System.err:    at br.com.gomus.androidapp.data.local.dao.schedule.SpotScheduleDaoImpl$1.run(SpotScheduleDaoImpl.java:113)
12-17 20:07:30.829  6387  8777 W System.err:    at android.os.Handler.handleCallback(Handler.java:755)
12-17 20:07:30.829  6387  8777 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 20:07:30.829  6387  8777 W System.err:    at android.os.Looper.loop(Looper.java:154)
12-17 20:07:30.830  6387  8777 W System.err:    at android.os.HandlerThread.run(HandlerThread.java:61)

这是 SpotScheduleDaoImp.java ,第113行对应于mDatabase.findRealmObject(ScheduleCompound.class, query,行。

@Override
public void find(Func1<RealmQuery<ScheduleCompound>, RealmQuery<ScheduleCompound>> query,
                 Action1<Result<List<ScheduleCompound>>> onResult) {
    if (query != null) {


        handlerThread = new HandlerThread("MyHandlerThreadspotscheduleDao");
        handlerThread.start();
        looper = handlerThread.getLooper();

        Handler handler = new Handler(looper);

        handler.post(new Runnable(){
            @Override
            public void run() {
                Timber.log(Log.DEBUG, "find Thread.currentThread().getName() " + Thread.currentThread().getName());

                mDatabase.findRealmObject(ScheduleCompound.class, query,
                        result -> {

                            onResult.call(Result.getData(SpotScheduleMapper.copyFromRealmResults(result.data)));
                            //handlerThread.quit();

                        },

                        err -> {
                            onResult.call(Result.getError(err));
                            //handlerThread.quit();

                        }
                );
            }
        });
    }
}

这是 LocalDatabaseImpl.java ,第 284 行对应于realm = Realm.getDefaultInstance();命令。

public <T extends RealmModel> void findRealmObject(Class<T> clazz,
                                                       Func1<RealmQuery<T>, RealmQuery<T>> filter,
                                                       Action1<Result<RealmResults<T>>> onResult,
                                                       Action1<Throwable> onError) {

        Realm realm = null;
        RealmResults<T> realmResults = null;
        try {
            realm = Realm.getDefaultInstance();

            RealmQuery<T> realmQuery = filter.call(realm.where(clazz));

            Timber.log(Log.DEBUG, "findRealmObject Thread.currentThread().getName() " + Thread.currentThread().getName());

            realmResults = realmQuery.findAll();

            if(realmResults.isValid()) {
                Timber.log(Log.DEBUG, clazz.getCanonicalName() + "findRealmObject realmQuery.findAll = " + realmResults);
                onResult.call(Result.getData(realmResults));

            }

        } catch (Exception e) {

            e.printStackTrace();
            onError.call(e);
        }
        finally
        {
            if(realm != null)
                if(realmResults != null && realmResults.isValid())
                    if(!realm.isClosed()) {
                        realm.close();
                    }
        }
    }

我了解到关闭领域实例有问题,但是我不知道为什么。因为在finally块中,我关闭了领域实例。在此之前,我在try和catch块的末尾都通过检查!realm.isClosed()之前是否未关闭来关闭两者,但是那时它给出了错误,并说领域实例已经关闭,因此无法使用。所以我将结束部分移到了finally块。

这是另一个堆栈跟踪。

--------- beginning of crash
12-17 20:07:31.796  6387  8776 E AndroidRuntime: FATAL EXCEPTION: MyHandlerThreadplaylistScheduleDao
12-17 20:07:31.796  6387  8776 E AndroidRuntime: Process: br.com.gomus.androidapp, PID: 6387
12-17 20:07:31.796  6387  8776 E AndroidRuntime: io.realm.exceptions.RealmError: Unrecoverable error. Too many open files in /Users/cm/Realm/realm-java-release/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 101
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:184)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:254)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:244)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:319)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at io.realm.Realm.getDefaultInstance(Realm.java:332)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at br.com.gomus.androidapp.data.local.database.LocalDatabaseImpl.findRealmObject(LocalDatabaseImpl.java:284)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at br.com.gomus.androidapp.data.local.dao.schedule.PlaylistScheduleDaoImpl$1.run(PlaylistScheduleDaoImpl.java:110)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:755)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:154)
12-17 20:07:31.796  6387  8776 E AndroidRuntime:    at android.os.HandlerThread.run(HandlerThread.java:61)

我在关闭领域实例时遇到麻烦。任何帮助将不胜感激。

谢谢。

0 个答案:

没有答案