2017年1月26日 星期四

How to use Realm

Intsall



Create Models


public class User extends RealmObject {

   private String          name;
   private int             age;

   @Ignore
   private int             sessionId;

   // Standard getters & setters generated by your IDE…
   public String getName() { return name; }
   public void   setName(String name) { this.name = name; }
   public int    getAge() { return age; }
   public void   setAge(int age) { this.age = age; }
   public int    getSessionId() { return sessionId; }
   public void   setSessionId(int sessionId) { this.sessionId = sessionId; }
}


How to write

Realm realm = Realm.getDefaultInstance();

way 1

realm.beginTransaction();
User user = realm.createObject(User.class); // Create a new object
user.setName("John");
user.setEmail("john@corporation.com");
realm.commitTransaction();


way 2

not block the UI thread
realm.executeTransactionAsync(new Realm.Transaction() {
           @Override
           public void execute(Realm bgRealm) {
               User user = bgRealm.createObject(User.class);
               user.setName("John");
               user.setEmail("john@corporation.com");
           }
       }, new Realm.Transaction.OnSuccess() {
           @Override
           public void onSuccess() {
               // Transaction was a success.
           }
       }, new Realm.Transaction.OnError() {
           @Override
           public void onError(Throwable error) {
               // Transaction failed and was automatically canceled.
           }
       });

if use primary key


@PrimaryKey
   private String name;


final String name = edName.getText().toString();
               final String old = edOld.getText().toString();

               Realm.Transaction transaction = new Realm.Transaction() {
                   @Override
                   public void execute(Realm bgrealm) {

                       User user = new User(name, old);
                       bgrealm.copyToRealmOrUpdate(user);

                   }
               };

How to query

RealmResults<User> result2 = realm.where(User.class)
                                 .equalTo("name", "John")
                                 .or()
                                 .equalTo("name", "Peter")
                                 .findAll();

result.get(result.size() - 1);


condition

  • between(), greaterThan(), lessThan(), greaterThanOrEqualTo() & lessThanOrEqualTo()
  • equalTo() & notEqualTo()
  • contains(), beginsWith() & endsWith()
  • isNull() & isNotNull()
  • isEmpty() & isNotEmpty()


and   or

Each condition is implicitly logical-and together. Logical-or must be applied explicitly with or().


RealmResults<User> r = realm.where(User.class)
                           .greaterThan("age", 10)  //implicit AND
                           .beginGroup()
                               .equalTo("name", "Peter")
                               .or()
                               .contains("name", "Jo")
                           .endGroup()
                           .findAll();


other…

Sorting

RealmResults<User> result = realm.where(User.class).findAll();
result.sort("age"); // Sort ascending
result.sort("age", RealmResults.SORT_ORDER_DESCENDING);



Auto-Updating Results
auto-updating views into the underlying data, which means results never have to be re-fetched



result = realm.where(User.class).findAll();
    

       result.addChangeListener(new RealmChangeListener<RealmResults<User>>() {
           @Override
           public void onChange(RealmResults<User> element) {
               useResultToSetTheUi(element);
           }
       });




Delete


// obtain the results of a query
final RealmResults<Dog> results = realm.where(Dog.class).findAll();

// All changes to data must happen in a transaction
realm.executeTransaction(new Realm.Transaction() {
   @Override
   public void execute(Realm realm) {
       // remove single match
       results.deleteFirstFromRealm();
       results.deleteLastFromRealm();

       // remove a single object
       Dog dog = results.get(5);
       dog.deleteFromRealm();

       // Delete all matches
       results.deleteAllFromRealm();
   }
});



Thread

Using a Realm across Threads
The only rule to using Realm across threads is to remember that Realm, RealmObject or RealmResults instances cannot be passed across threads

Learn how to work with Android




use with RxJava




2017年1月11日 星期三

Android handle Android 6.0 permission

PROTECTION_NORMAL类权限

当用户安装或更新应用时,系统将授予应用所请求的属于 PROTECTION_NORMAL 的所有权限(安装时授权的一类基本权限)。这类权限包括
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
The permission that need to handle at runtime

use library:


example