Source:
<code>static synchronized <E extends BaseRealm> E createRealmOrGetFromCache(RealmConfiguration configuration, Class<E> realmClass) { boolean isCacheInMap = true; RealmCache cache = cachesMap.get(configuration.getPath()); if (cache == null) { cache = new RealmCache(configuration); isCacheInMap = false; copyAssetFileIfNeeded(configuration); } else { cache.validateConfiguration(configuration); } RefAndCount refAndCount = cache.refAndCountMap.get(RealmCacheType.valueOf(realmClass)); if (refAndCount.globalCount == 0) { SharedRealm sharedRealm = SharedRealm.getInstance(configuration); sharedRealm.beginTransaction(); <START> if (Table.migratePrimaryKeyTableIfNeeded(sharedRealm)) { <END> sharedRealm.commitTransaction(); } else { sharedRealm.cancelTransaction(); } sharedRealm.close(); } if (refAndCount.localRealm.get() == null) { BaseRealm realm; if (realmClass == Realm.class) { realm = Realm.createInstance(configuration, cache.typedColumnIndicesArray); } else if (realmClass == DynamicRealm.class) { realm = DynamicRealm.createInstance(configuration); } else { throw new IllegalArgumentException(WRONG_REALM_CLASS_MESSAGE); } if (!isCacheInMap) { cachesMap.put(configuration.getPath(), cache); } refAndCount.localRealm.set(realm); refAndCount.localCount.set(0); } Integer refCount = refAndCount.localCount.get(); if (refCount == 0) { if (realmClass == Realm.class && refAndCount.globalCount == 0) { final BaseRealm realm = refAndCount.localRealm.get(); RealmCache.storeColumnIndices(cache.typedColumnIndicesArray, realm.schema.columnIndices.clone()); } refAndCount.globalCount++; } refAndCount.localCount.set(refCount + 1); @SuppressWarnings("unchecked") E realm = (E) refAndCount.localRealm.get(); return realm; }</code><technical_language>Is a check if need migrate a transaction. Transactions init pretty dangerous in multi-process scenarios, happen UI thread</technical_language>

Target:
static synchronized <E extends BaseRealm> E createRealmOrGetFromCache(RealmConfiguration configuration, Class<E> realmClass) { boolean isCacheInMap = true; RealmCache cache = cachesMap.get(configuration.getPath()); if (cache == null) { cache = new RealmCache(configuration); isCacheInMap = false; copyAssetFileIfNeeded(configuration); } else { cache.validateConfiguration(configuration); } RefAndCount refAndCount = cache.refAndCountMap.get(RealmCacheType.valueOf(realmClass)); if (refAndCount.globalCount == 0) { SharedRealm sharedRealm = SharedRealm.getInstance(configuration); if (Table.primaryKeyTableNeedsMigration(sharedRealm)) { sharedRealm.beginTransaction(); if (Table.migratePrimaryKeyTableIfNeeded(sharedRealm)) { sharedRealm.commitTransaction(); } else { sharedRealm.cancelTransaction(); } } sharedRealm.close(); } if (refAndCount.localRealm.get() == null) { BaseRealm realm; if (realmClass == Realm.class) { realm = Realm.createInstance(configuration, cache.typedColumnIndicesArray); } else if (realmClass == DynamicRealm.class) { realm = DynamicRealm.createInstance(configuration); } else { throw new IllegalArgumentException(WRONG_REALM_CLASS_MESSAGE); } if (!isCacheInMap) { cachesMap.put(configuration.getPath(), cache); } refAndCount.localRealm.set(realm); refAndCount.localCount.set(0); } Integer refCount = refAndCount.localCount.get(); if (refCount == 0) { if (realmClass == Realm.class && refAndCount.globalCount == 0) { final BaseRealm realm = refAndCount.localRealm.get(); RealmCache.storeColumnIndices(cache.typedColumnIndicesArray, realm.schema.columnIndices.clone()); } refAndCount.globalCount++; } refAndCount.localCount.set(refCount + 1); @SuppressWarnings("unchecked") E realm = (E) refAndCount.localRealm.get(); return realm; }