Search code examples
androidkotlinrealmrealm-mobile-platform

Realm database getGlobalInstanceCount() and getLocalInstanceCount() getting increasing until kill the application


Realm database GlobalInstanceCount and LocalInstanceCount goes on increasing until kill the application. Bcz of this facing out of memory error . Our application using many insert and update operation in both main thread and background thread. But after once work is finishes doing close() realm instances successfully. In spite of close in the both main thread and background thread also global and local count not getting reducing. Please help me thank you in advance.

var locCount = Realm.getLocalInstanceCount(Realm.getDefaultConfiguration())
var globalCount = Realm.getGlobalInstanceCount(Realm.getDefaultConfiguration())
Log.d("LogCount","Global Count= $globalCount , Local Count= $locCount")

Below singleton class using for each database operations. That is same instance using for all the operation not creating new instance.

  object RealMDatabase {

    fun getRealm(): Realm {
        return if (Looper.getMainLooper().thread == Thread.currentThread()) {
            if (!::realm_main.isInitialized || realm_main.isClosed) {
                realm_main = Realm.getDefaultInstance()
            }
            realm_main

        } else {
            if (!::realm_io.isInitialized || realm_io.isClosed) {
                realm_io = Realm.getDefaultInstance()
            }
            realm_io
        }
     }
 }

Solution

  • So I'm guessing that your seats to cancel are coming in as a comma-separated list from the URL. So all you need to do is explode() the GET parameter to turn it into an array, place it as the value to a key and json_encode() it.

    i.e.

    $seatsToCancel = explode(',', $_GET['seatsToCancel']);
    $params = [
        'tin' => $tin,
        'seatsToCancel' => $seatsToCancel,
    ];
    $paramsJson = json_encode($params);
    

    That should get you the result you're after:

    {"tin":"S4243534" ,"seatsToCancel":["U23","L43","U12"]}
    

    You may want to consider what happens if the GET parameter is empty and whether or not you want any additional error checking.