Search in sources :

Example 61 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class OsObjectStoreTests method callWithLock_throwInCallback.

// Test if a java exception can be thrown from the callback.
@Test
public void callWithLock_throwInCallback() {
    RealmConfiguration config = configFactory.createConfiguration();
    final RuntimeException exception = new RuntimeException();
    try {
        OsObjectStore.callWithLock(config, new Runnable() {

            @Override
            public void run() {
                throw exception;
            }
        });
        fail();
    } catch (RuntimeException e) {
        assertEquals(exception, e);
    }
    // The lock should be released after exception thrown
    final AtomicBoolean callbackCalled = new AtomicBoolean(false);
    assertTrue(OsObjectStore.callWithLock(config, new Runnable() {

        @Override
        public void run() {
            callbackCalled.set(true);
        }
    }));
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 62 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class RunInLooperThread method before.

@Override
protected void before() throws Throwable {
    super.before();
    RealmConfiguration config = createConfiguration(UUID.randomUUID().toString());
    List<Object> refs = new ArrayList<>();
    List<Realm> realms = new ArrayList<>();
    List<Closeable> closeables = new ArrayList<>();
    synchronized (lock) {
        realmConfiguration = config;
        realm = null;
        backgroundHandler = null;
        keepStrongReference = refs;
        testRealms = realms;
        closableResources = closeables;
    }
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Realm(io.realm.Realm)

Example 63 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class ModulesExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modules_example);
    rootLayout = ((LinearLayout) findViewById(R.id.container));
    rootLayout.removeAllViews();
    // The default Realm instance implicitly knows about all classes in the realmModuleAppExample Android Studio
    // module. This does not include the classes from the realmModuleLibraryExample AS module so a Realm using this
    // configuration would know about the following classes: { Cow, Pig, Snake, Spider }
    RealmConfiguration defaultConfig = new RealmConfiguration.Builder().build();
    // It is possible to extend the default schema by adding additional Realm modules using modules(). This can
    // also be Realm modules from libraries. The below Realm contains the following classes: { Cow, Pig, Snake,
    // Spider, Cat, Dog }
    RealmConfiguration farmAnimalsConfig = new RealmConfiguration.Builder().name("farm.realm").modules(Realm.getDefaultModule(), new DomesticAnimalsModule()).build();
    // Or you can completely replace the default schema.
    // This Realm contains the following classes: { Elephant, Lion, Zebra, Snake, Spider }
    RealmConfiguration exoticAnimalsConfig = new RealmConfiguration.Builder().name("exotic.realm").modules(new ZooAnimalsModule(), new CreepyAnimalsModule()).build();
    // Multiple Realms can be opened at the same time
    showStatus("Opening multiple Realms");
    Realm defaultRealm = Realm.getInstance(defaultConfig);
    final Realm farmRealm = Realm.getInstance(farmAnimalsConfig);
    Realm exoticRealm = Realm.getInstance(exoticAnimalsConfig);
    // Objects can be added to each Realm independantly
    showStatus("Create objects in the default Realm");
    defaultRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Cow.class);
            realm.createObject(Pig.class);
            realm.createObject(Snake.class);
            realm.createObject(Spider.class);
        }
    });
    showStatus("Create objects in the farm Realm");
    farmRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Cow.class);
            realm.createObject(Pig.class);
            realm.createObject(Cat.class);
            realm.createObject(Dog.class);
        }
    });
    showStatus("Create objects in the exotic Realm");
    exoticRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Elephant.class);
            realm.createObject(Lion.class);
            realm.createObject(Zebra.class);
            realm.createObject(Snake.class);
            realm.createObject(Spider.class);
        }
    });
    // You can copy objects between Realms
    showStatus("Copy objects between Realms");
    showStatus("Number of pigs on the farm : " + farmRealm.where(Pig.class).count());
    showStatus("Copy pig from defaultRealm to farmRealm");
    final Pig defaultPig = defaultRealm.where(Pig.class).findFirst();
    farmRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.copyToRealm(defaultPig);
        }
    });
    showStatus("Number of unnamed pigs on the farm : " + farmRealm.where(Pig.class).isNull("name").count());
    // Each Realm is restricted to only accept the classes in their schema.
    showStatus("Trying to add an unsupported class");
    defaultRealm.beginTransaction();
    try {
        defaultRealm.createObject(Elephant.class);
    } catch (RealmException expected) {
        showStatus("This throws a :" + expected.toString());
    } finally {
        defaultRealm.cancelTransaction();
    }
    // And Realms in library projects are independent from Realms in the app code
    showStatus("Interacting with library code that uses Realm internally");
    int animals = 5;
    Zoo libraryZoo = new Zoo();
    libraryZoo.open();
    showStatus("Adding animals: " + animals);
    libraryZoo.addAnimals(5);
    showStatus("Number of animals in the library Realm:" + libraryZoo.getNoOfAnimals());
    libraryZoo.close();
    // Remember to close all open Realms
    defaultRealm.close();
    farmRealm.close();
    exoticRealm.close();
}
Also used : ZooAnimalsModule(io.realm.examples.librarymodules.modules.ZooAnimalsModule) CreepyAnimalsModule(io.realm.examples.appmodules.modules.CreepyAnimalsModule) Snake(io.realm.examples.appmodules.model.Snake) Zebra(io.realm.examples.librarymodules.model.Zebra) Cow(io.realm.examples.appmodules.model.Cow) DomesticAnimalsModule(io.realm.examples.librarymodules.modules.DomesticAnimalsModule) RealmException(io.realm.exceptions.RealmException) Pig(io.realm.examples.appmodules.model.Pig) RealmConfiguration(io.realm.RealmConfiguration) Cat(io.realm.examples.librarymodules.model.Cat) Elephant(io.realm.examples.librarymodules.model.Elephant) Spider(io.realm.examples.appmodules.model.Spider) Lion(io.realm.examples.librarymodules.model.Lion) Zoo(io.realm.examples.librarymodules.Zoo) Realm(io.realm.Realm) Dog(io.realm.examples.librarymodules.model.Dog) LinearLayout(android.widget.LinearLayout)

Example 64 with RealmConfiguration

use of io.realm.RealmConfiguration in project realm-java by realm.

the class EncryptionExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Generate a key
    // IMPORTANT! This is a silly way to generate a key. It is also never stored.
    // For proper key handling please consult:
    // * https://developer.android.com/training/articles/keystore.html
    // * http://nelenkov.blogspot.dk/2012/05/storing-application-secrets-in-androids.html
    byte[] key = new byte[64];
    new SecureRandom().nextBytes(key);
    // An encrypted Realm file can be opened in Realm Studio by using a Hex encoded version
    // of the key. Copy the key from Logcat, then download the Realm file from the device using
    // the method described here: https://stackoverflow.com/a/28486297/1389357
    // The path is normally `/data/data/io.realm.examples.encryption/files/default.realm`
    Log.i("RealmEncryptionKey", Util.bytesToHex(key));
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().encryptionKey(key).build();
    // Start with a clean slate every time
    Realm.deleteRealm(realmConfiguration);
    // Open the Realm with encryption enabled
    realm = Realm.getInstance(realmConfiguration);
    // Everything continues to work as normal except for that the file is encrypted on disk
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            Person person = realm.createObject(Person.class);
            person.setName("Happy Person");
            person.setAge(14);
        }
    });
    Person person = realm.where(Person.class).findFirst();
    Log.i(TAG, String.format("Person name: %s", person.getName()));
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) SecureRandom(java.security.SecureRandom) Realm(io.realm.Realm)

Aggregations

RealmConfiguration (io.realm.RealmConfiguration)64 Realm (io.realm.Realm)20 DynamicRealm (io.realm.DynamicRealm)17 Scheduler (io.reactivex.Scheduler)14 RealmChangeListener (io.realm.RealmChangeListener)8 BeforeExperiment (dk.ilios.spanner.BeforeExperiment)6 Before (org.junit.Before)5 DynamicRealmObject (io.realm.DynamicRealmObject)4 RealmList (io.realm.RealmList)4 RealmResults (io.realm.RealmResults)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Context (android.content.Context)3 OrderedCollectionChangeSet (io.realm.OrderedCollectionChangeSet)3 OrderedRealmCollectionChangeListener (io.realm.OrderedRealmCollectionChangeListener)3 RealmObjectSchema (io.realm.RealmObjectSchema)3 AllTypes (io.realm.entities.AllTypes)3 IntentFilter (android.content.IntentFilter)2 LinearLayout (android.widget.LinearLayout)2 RealmSchema (io.realm.RealmSchema)2