Search in sources :

Example 56 with RealmConfiguration

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

the class MyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    Realm.init(this);
    RealmConfiguration configuration = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build();
    Realm.setDefaultConfiguration(configuration);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration)

Example 57 with RealmConfiguration

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

the class MyApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    // Configure Realm for the application
    Realm.init(this);
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
    // Clean slate
    Realm.deleteRealm(realmConfiguration);
    // Make this Realm the default
    Realm.setDefaultConfiguration(realmConfiguration);
}
Also used : RealmConfiguration(io.realm.RealmConfiguration)

Example 58 with RealmConfiguration

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

the class JsonExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_example);
    RealmConfiguration config = new RealmConfiguration.Builder().allowWritesOnUiThread(true).allowQueriesOnUiThread(true).build();
    Realm.deleteRealm(config);
    realm = Realm.getInstance(config);
    gridView = findViewById(R.id.cities_list);
    cities = realm.where(City.class).findAllAsync();
    cities.addChangeListener(realmChangeListener);
    adapter = new CityAdapter();
    gridView.setAdapter(adapter);
    // Load from file "cities.json" first time
    loadCities();
}
Also used : RealmConfiguration(io.realm.RealmConfiguration)

Example 59 with RealmConfiguration

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

the class MigrationExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_migration_example);
    rootLayout = ((LinearLayout) findViewById(R.id.container));
    rootLayout.removeAllViews();
    // 3 versions of the databases for testing. Normally you would only have one.
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default0), "default0.realm");
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default1), "default1.realm");
    copyBundledRealmFile(this.getResources().openRawResource(R.raw.default2), "default2.realm");
    // When you create a RealmConfiguration you can specify the version of the schema.
    // If the schema does not have that version a RealmMigrationNeededException will be thrown.
    RealmConfiguration config0 = new RealmConfiguration.Builder().name("default0.realm").schemaVersion(3).build();
    // You can then manually call Realm.migrateRealm().
    try {
        Realm.migrateRealm(config0, new Migration());
    } catch (FileNotFoundException ignored) {
    // If the Realm file doesn't exist, just ignore.
    }
    realm = Realm.getInstance(config0);
    showStatus("Default0");
    showStatus(realm);
    realm.close();
    // Or you can add the migration code to the configuration. This will run the migration code without throwing
    // a RealmMigrationNeededException.
    RealmConfiguration config1 = new RealmConfiguration.Builder().name("default1.realm").schemaVersion(3).migration(new Migration()).build();
    // Automatically run migration if needed
    realm = Realm.getInstance(config1);
    showStatus("Default1");
    showStatus(realm);
    realm.close();
    // or you can set .deleteRealmIfMigrationNeeded() if you don't want to bother with migrations.
    // WARNING: This will delete all data in the Realm though.
    RealmConfiguration config2 = new RealmConfiguration.Builder().name("default2.realm").schemaVersion(3).deleteRealmIfMigrationNeeded().build();
    realm = Realm.getInstance(config2);
    showStatus("default2");
    showStatus(realm);
    realm.close();
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) Migration(io.realm.examples.realmmigrationexample.model.Migration) FileNotFoundException(java.io.FileNotFoundException) LinearLayout(android.widget.LinearLayout)

Example 60 with RealmConfiguration

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

the class ExampleActivityTest method setup.

@Before
public void setup() throws Exception {
    // Setup Realm to be mocked. The order of these matters
    mockStatic(RealmCore.class);
    mockStatic(RealmLog.class);
    mockStatic(Realm.class);
    mockStatic(RealmConfiguration.class);
    Realm.init(RuntimeEnvironment.application);
    // Create the mock
    final Realm mockRealm = mock(Realm.class);
    final RealmConfiguration mockRealmConfig = mock(RealmConfiguration.class);
    // TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some
    // problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which
    // will be called by RealmConfiguration.Builder's constructor.
    doNothing().when(RealmCore.class);
    RealmCore.loadLibrary(any(Context.class));
    // TODO: Mock the RealmConfiguration's constructor. If the RealmConfiguration.Builder.build can be mocked, this
    // is not necessary anymore.
    whenNew(RealmConfiguration.class).withAnyArguments().thenReturn(mockRealmConfig);
    // Anytime getInstance is called with any configuration, then return the mockRealm
    when(Realm.getDefaultInstance()).thenReturn(mockRealm);
    // Anytime we ask Realm to create a Person, return a new instance.
    when(mockRealm.createObject(Person.class)).thenReturn(new Person());
    // Set up some naive stubs
    Person p1 = new Person();
    p1.setAge(14);
    p1.setName("John Young");
    Person p2 = new Person();
    p2.setAge(89);
    p2.setName("John Senior");
    Person p3 = new Person();
    p3.setAge(27);
    p3.setName("Jane");
    Person p4 = new Person();
    p4.setAge(42);
    p4.setName("Robert");
    List<Person> personList = Arrays.asList(p1, p2, p3, p4);
    // Create a mock RealmQuery
    RealmQuery<Person> personQuery = mockRealmQuery();
    // When the RealmQuery performs findFirst, return the first record in the list.
    when(personQuery.findFirst()).thenReturn(personList.get(0));
    // When the where clause is called on the Realm, return the mock query.
    when(mockRealm.where(Person.class)).thenReturn(personQuery);
    // When the RealmQuery is filtered on any string and any integer, return the person query
    when(personQuery.equalTo(anyString(), anyInt())).thenReturn(personQuery);
    // RealmResults is final, must mock static and also place this in the PrepareForTest annotation array.
    mockStatic(RealmResults.class);
    // Create a mock RealmResults
    RealmResults<Person> people = mockRealmResults();
    // When we ask Realm for all of the Person instances, return the mock RealmResults
    when(mockRealm.where(Person.class).findAll()).thenReturn(people);
    // When a between query is performed with any string as the field and any int as the
    // value, then return the personQuery itself
    when(personQuery.between(anyString(), anyInt(), anyInt())).thenReturn(personQuery);
    // When a beginsWith clause is performed with any string field and any string value
    // return the same person query
    when(personQuery.beginsWith(anyString(), anyString())).thenReturn(personQuery);
    // When we ask the RealmQuery for all of the Person objects, return the mock RealmResults
    when(personQuery.findAll()).thenReturn(people);
    // The for(...) loop in Java needs an iterator, so we're giving it one that has items,
    // since the mock RealmResults does not provide an implementation. Therefore, anytime
    // anyone asks for the RealmResults Iterator, give them a functioning iterator from the
    // ArrayList of Persons we created above. This will allow the loop to execute.
    when(people.iterator()).thenReturn(personList.iterator());
    // Return the size of the mock list.
    when(people.size()).thenReturn(personList.size());
    this.mockRealm = mockRealm;
    this.people = people;
}
Also used : Context(android.content.Context) RealmConfiguration(io.realm.RealmConfiguration) Realm(io.realm.Realm) Person(io.realm.examples.unittesting.model.Person) Before(org.junit.Before)

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