簡體   English   中英

在遞歸方法中使用查詢時出現StackOverflowError

[英]StackOverflowError when using a query in a recursive method

在本地數據庫中,我有記錄,該記錄包含有關目錄和文件(文件系統樹)的信息。 例如,表“ Dir”具有屬性: record_id (autoincr。), dir_iddirnameparentDir_id 我需要從數據庫創建文件和目錄樹:

Dir_1
|
 -----Dir_1.1
 -----Dir_1.2
 -----File_1.1
Dir_2
|
 -----Dir_2.1
 -----Dir_2.2
 -----File_2.1
 -----File_2.2
Dir_3
File_1
File_2

因此,我使用遞歸方法。 我的功能:

 /**
 * Append new childs for target dir
 * @param targetObject directory
 */
public void addChilds(@Nullable SimpleSyncDirModel targetObject){
    //no target - main parent dir.
    if (targetObject == null){
        //get childs for this target object (main dir without parent)
        ArrayList<Object> foundedChilds = ThisApp.getDBhelper().getSharedSimpleObjects(this.dir_id);
        //step by step (childs). recursive mecthod.
        for (Object thisChild : foundedChilds){
            if (thisChild instanceof SimpleSyncDirModel){
                addNewChildDir((SimpleSyncDirModel) thisChild);
                //find and add childs for current child
                addChilds((SimpleSyncDirModel) thisChild);
            }else if (thisChild instanceof SimpleSyncFileModel){
                addNewChildFile((SimpleSyncFileModel) thisChild);
            }
        }
    }else{
        //get childs for this target object (not main, has parent)
        ArrayList<Object> foundedChilds = ThisApp.getDBhelper().getSharedSimpleObjects(targetObject.dir_id);
        //iterate, find and append childs (recursive)
        for (Object thisChild : foundedChilds){
            if (thisChild instanceof SimpleSyncDirModel){
                targetObject.addNewChildDir((SimpleSyncDirModel) thisChild);
                //recursive
                addChilds((SimpleSyncDirModel) thisChild);
            }else if (thisChild instanceof SimpleSyncFileModel){
                targetObject.addNewChildFile((SimpleSyncFileModel) thisChild);
            }
        }

    }
}

如您所見,我使用函數

ThisApp.getDBhelper()。getSharedSimpleObjects(targetObject.dir_id);

用於搜索目標目錄的子目錄。 targetObject.dir_id- “選擇”的parent_id。

我的搜索子函數:

 /**
 * Get childs with parent_id == 0 (without parent) or not (with parent)
 * @param parendDir_id ID of dir, for what we search childs
 * @return list of childs like {@link SimpleSyncDirModel}
 * and {@link ru.rsit.megashare.models.global.SimpleSyncFileModel}
 */
public ArrayList<Object> getSharedSimpleObjects(String parendDir_id){

    ArrayList<Object> result = new ArrayList<>();

    //looking child dirs
    String[] columns_ = new String[]{
            "dir_id", "dirnameWithPath"
    };
    Cursor db_cursor = megashare_db.query(
            "SharedDir", columns_,
            "(parentDir_id = ?)",
            new String[] { parendDir_id },
            null, null, null);
    if (db_cursor.getCount() > 0) {
        db_cursor.moveToFirst();
        while (db_cursor.getPosition() != db_cursor.getCount()) {
            SimpleSyncDirModel simpleSyncDirModel = new SimpleSyncDirModel();
            simpleSyncDirModel.dir_id =
                    Integer.toString(db_cursor.getInt(db_cursor.getColumnIndex("dir_id")));
            simpleSyncDirModel.dirnameWithPath =
                    db_cursor.getString(db_cursor.getColumnIndex("dirnameWithPath"));
            result.add(simpleSyncDirModel);
            db_cursor.moveToNext();
        }
    }
    db_cursor.close();

    //looking child files
    columns_ = new String[]{
            "file_id", "filenameWithPath"
    };
    db_cursor = megashare_db.query(
            "SharedFile", columns_,
            "(parentDir_id = ?)",
            new String[] { parendDir_id },
            null, null, null);
    if (db_cursor.getCount() > 0) {
        db_cursor.moveToFirst();
        while (db_cursor.getPosition() != db_cursor.getCount()) {
            SimpleSyncFileModel simpleSyncFileModel = new SimpleSyncFileModel();
            simpleSyncFileModel.file_id =
                    Integer.toString(db_cursor.getInt(db_cursor.getColumnIndex("file_id")));
            simpleSyncFileModel.filenameWithPath =
                    db_cursor.getString(db_cursor.getColumnIndex("filenameWithPath"));
            result.add(simpleSyncFileModel);
            db_cursor.moveToNext();
        }
    }
    db_cursor.close();

    return result;
}

當我運行應用程序並嘗試創建文件系統樹時,總是看到java.lang.StackOverflowError

FATAL EXCEPTION: Thread-23110
 java.lang.StackOverflowError
     at java.lang.ref.WeakReference.<init>(WeakReference.java:108)
     at java.util.WeakHashMap$Entry.<init>(WeakHashMap.java:71)
     at java.util.WeakHashMap.put(WeakHashMap.java:611)
     at android.database.sqlite.SQLiteConnectionPool.finishAcquireConnectionLocked(SQLiteConnectionPool.java:980)
     at android.database.sqlite.SQLiteConnectionPool.tryAcquirePrimaryConnectionLocked(SQLiteConnectionPool.java:916)
     at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:682)
     at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:400)
     at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:905)
     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1283)
     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1154)
     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1322)
     at ru.rsit.megashare.managers.DBHelper.getSharedSimpleObjects(DBHelper.java:582)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:64)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
     at ru.rsit.megashare.models.global.SimpleSyncDirModel.addChilds(SimpleSyncDirModel.java:70)
    at

錯誤在這里: Cursor db_cursor = megashare_db.query

為什么我有這個錯誤? 當然我總是關閉光標。 解決此問題我需要做什么?

錯誤的原因是代碼錯誤,我嘗試在數據庫目標dir中查找以進行檢查:此目錄沒有父目錄。 因此,如果我刪除子對象,我的應用程序將復制一些目錄。 這就是遞歸循環的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM