Skip to content

Instantly share code, notes, and snippets.

@brucetoo
Created August 18, 2015 12:58
Show Gist options
  • Select an option

  • Save brucetoo/f2361fad6361ad83b722 to your computer and use it in GitHub Desktop.

Select an option

Save brucetoo/f2361fad6361ad83b722 to your computer and use it in GitHub Desktop.
RealmModules的使用
/*
* Copyright 2015 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.brucetoo.realmdemo.realmappmodules;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.brucetoo.realmdemo.R;
import com.brucetoo.realmdemo.realmappmodules.model.Cow;
import com.brucetoo.realmdemo.realmappmodules.model.Pig;
import com.brucetoo.realmdemo.realmappmodules.model.Snake;
import com.brucetoo.realmdemo.realmappmodules.model.Spider;
import com.brucetoo.realmdemo.realmappmodules.modules.CreepyAnimalsModule;
import com.brucetoo.realmdemo.realmintro.model.Cat;
import com.brucetoo.realmdemo.realmintro.model.Dog;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.examples.librarymodules.Zoo;
import io.realm.examples.librarymodules.model.Elephant;
import io.realm.examples.librarymodules.model.Lion;
import io.realm.examples.librarymodules.model.Zebra;
import io.realm.examples.librarymodules.modules.DomesticAnimalsModule;
import io.realm.examples.librarymodules.modules.ZooAnimalsModule;
import io.realm.exceptions.RealmException;
/**
* This example demonstrates how you can use modules to control which classes belong to which Realms and how you can
* work with multiple Realms at the same time.
*/
public class ModulesExampleActivity extends Activity {
public static final String TAG = ModulesExampleActivity.class.getName();
private LinearLayout rootLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modules_example);
rootLayout = ((LinearLayout) findViewById(R.id.container));
rootLayout.removeAllViews();
/**
* 在默认情况下,Realm的实例只能使用android studio主工程的Module中的类,
* 其他的Module则不能使用,为了使用其他library Module的类,这配置Module则是必须的
*
* Module的定义
1.首先必须用注解@RealModule来标识Module 参数 library = true
2.第二个参数表示该声明的Module能使用那些Model
allClasses = true 表示该Module能使用与其在同一个android studio Module中所有的类
@RealmModule(library = true, allClasses = true)
public class AllAnimalsModule {
}
//
@RealmModule(library = true, classes = {Cat.class, Dog.class})
public class DomesticAnimalsModule {
}
setModules(Realm.getDefaultModule(), new DomesticAnimalsModule())
Realm.getDefaultModule() 是获取默认的Module
上面的意思是将后者Module可使用的类加入到默认可使用的类中
setModules(new ZooAnimalsModule(), new CreepyAnimalsModule())
表示替换默认的Module 只使用 ZooAnimalsModule 和 ZooAnimalsModule中包含的类
*/
// 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(this).build();
// It is possible to extend the default schema by adding additional Realm modules using setModule(). 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(this)
.name("farm.realm")
.setModules(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(this)
.name("exotic.realm")
.setModules(new ZooAnimalsModule(), new CreepyAnimalsModule())
.build();
// Multiple Realms can be open at the same time
/**
* 可同时操作不同的Module
*/
showStatus("Opening multiple Realms");
Realm defaultRealm = Realm.getInstance(defaultConfig);
Realm farmRealm = Realm.getInstance(farmAnimalsConfig);
Realm exoticRealm = Realm.getInstance(exoticAnimalsConfig);
/**
* 默认操作Realm是使用 realm.beginTransaction(), realm.commitTransaction()
* 也可使用代码快来代替,如下
* realm.executeTransaction(new Realm.Transaction)
*/
// 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);
}
});
/**
* 可以在不同的Module管理的Realm直接copy对象
*/
// 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");
Pig defaultPig = defaultRealm.where(Pig.class).findFirst();
farmRealm.beginTransaction();
farmRealm.copyToRealm(defaultPig);
farmRealm.commitTransaction();
showStatus("Number of pigs on the farm : " + farmRealm.where(Pig.class).count());
// Each Realm is restricted to only accept the classes in their schema.
/**
* 但是每个Realm只能创建在其控制范围内的类
*/
showStatus("Trying to add an unsupported class");
defaultRealm.beginTransaction();
try {
//defaultRealm 没有包含 Elephant所以在此会抛出异常
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
/**
* library中的Realm操作是和主app中的操作独立开的,因此可以单独操作
*/
showStatus("Interacting with library code that uses Realm internally");
int animals = 5;
Zoo libraryZoo = new Zoo(this);
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();
}
private void showStatus(String txt) {
Log.i(TAG, txt);
TextView tv = new TextView(this);
tv.setText(txt);
rootLayout.addView(tv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment