Search in sources :

Example 1 with Pair

use of io.realm.internal.util.Pair in project realm-java by realm.

the class SyncSession method removeProgressListener.

/**
 * Removes a progress listener. If the listener wasn't registered, this method will do nothing.
 *
 * @param listener listener to remove.
 */
public synchronized void removeProgressListener(ProgressListener listener) {
    // noinspection ConstantConditions
    if (listener == null) {
        return;
    }
    // If an exception is thrown somewhere in here, we will most likely leave the various
    // maps in an inconsistent manner. Not much we can do about it.
    Long token = progressListenerToOsTokenMap.remove(listener);
    if (token != null) {
        Iterator<Map.Entry<Long, Pair<ProgressListener, Progress>>> it = listenerIdToProgressListenerMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Long, Pair<ProgressListener, Progress>> entry = it.next();
            if (entry.getValue().first.equals(listener)) {
                it.remove();
                break;
            }
        }
        nativeRemoveProgressListener(appNativePointer, configuration.getPath(), token);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map) Pair(io.realm.internal.util.Pair)

Example 2 with Pair

use of io.realm.internal.util.Pair in project realm-java by realm.

the class RxJavaTests method realmResults_readableAcrossThreads.

@Test
@RunTestInLooperThread
public void realmResults_readableAcrossThreads() {
    final long TEST_SIZE = 10;
    Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    for (int i = 0; i < TEST_SIZE; i++) {
        realm.createObject(AllTypes.class).setColumnLong(1);
    }
    realm.commitTransaction();
    AtomicLong startingThread = new AtomicLong(Thread.currentThread().getId());
    AtomicLong subscriberThread = new AtomicLong();
    subscription = realm.where(AllTypes.class).sort(AllTypes.FIELD_LONG).findAllAsync().asFlowable().doOnSubscribe((r) -> {
        subscriberThread.set(Thread.currentThread().getId());
    }).subscribeOn(Schedulers.io()).filter(results -> {
        assertNotEquals(startingThread.get(), subscriberThread.get());
        return results.isLoaded();
    }).map(results -> new Pair<>(results.size(), new Pair<>(results, results.first()))).observeOn(Schedulers.computation()).subscribe(pair -> {
        assertNotEquals(startingThread.get(), Thread.currentThread().getId());
        assertNotEquals(subscriberThread.get(), Thread.currentThread().getId());
        assertEquals(TEST_SIZE, pair.first.intValue());
        assertEquals(TEST_SIZE, pair.second.first.size());
        assertEquals(pair.second.second.getColumnLong(), pair.second.first.first().getColumnLong());
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) AllTypes(io.realm.entities.AllTypes) Pair(io.realm.internal.util.Pair) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 3 with Pair

use of io.realm.internal.util.Pair in project realm-java by realm.

the class OsMap method getKeyRealmAnyPair.

public <K> Pair<K, NativeRealmAny> getKeyRealmAnyPair(int position) {
    // entry from OsMap is an array: entry[0] is key and entry[1] is a RealmAny value
    Object[] entry = nativeGetEntryForRealmAny(nativePtr, position);
    String key = (String) entry[0];
    NativeRealmAny nativeRealmAny = new NativeRealmAny((long) entry[1]);
    // noinspection unchecked
    return new Pair<>((K) key, nativeRealmAny);
}
Also used : NativeRealmAny(io.realm.internal.core.NativeRealmAny) Pair(io.realm.internal.util.Pair)

Example 4 with Pair

use of io.realm.internal.util.Pair in project realm-java by realm.

the class RealmTests method populateTestRealmAndCompactOnLaunch.

private Pair<Long, Long> populateTestRealmAndCompactOnLaunch(CompactOnLaunchCallback compactOnLaunch, int sizeInMB) {
    final String REALM_NAME = "test.realm";
    RealmConfiguration realmConfig = configFactory.createConfiguration(REALM_NAME);
    Realm realm = Realm.getInstance(realmConfig);
    populateTestRealmForCompact(realm, sizeInMB);
    realm.beginTransaction();
    realm.deleteAll();
    realm.commitTransaction();
    realm.close();
    long before = new File(realmConfig.getPath()).length();
    if (compactOnLaunch != null) {
        realmConfig = configFactory.createConfigurationBuilder().name(REALM_NAME).compactOnLaunch(compactOnLaunch).build();
    } else {
        realmConfig = configFactory.createConfigurationBuilder().name(REALM_NAME).compactOnLaunch().build();
    }
    realm = Realm.getInstance(realmConfig);
    realm.close();
    long after = new File(realmConfig.getPath()).length();
    return new Pair(before, after);
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) OsSharedRealm(io.realm.internal.OsSharedRealm) File(java.io.File) Pair(io.realm.internal.util.Pair)

Aggregations

Pair (io.realm.internal.util.Pair)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 SystemClock (android.os.SystemClock)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 Flowable (io.reactivex.Flowable)1 Disposable (io.reactivex.disposables.Disposable)1 Consumer (io.reactivex.functions.Consumer)1 Schedulers (io.reactivex.schedulers.Schedulers)1 AllJavaTypes (io.realm.entities.AllJavaTypes)1 AllTypes (io.realm.entities.AllTypes)1 CyclicType (io.realm.entities.CyclicType)1 Dog (io.realm.entities.Dog)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1 OsSharedRealm (io.realm.internal.OsSharedRealm)1 NativeRealmAny (io.realm.internal.core.NativeRealmAny)1 RealmLog (io.realm.log.RealmLog)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)1 File (java.io.File)1