0

我有一个 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)

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

谢谢。

4

0 回答 0