Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realm Meetup #19 (Java)

zaki50
September 29, 2016

Realm Meetup #19 (Java)

zaki50

September 29, 2016
Tweet

More Decks by zaki50

Other Decks in Technology

Transcript

  1. Ϗϧυํ๏ [email protected] how-to-build.md ࢀর • Ubuntu 10.04, 12.04, 13.04, 13.10,

    14.04 • Linux Mint 15, 16, 17, 17.1 • Fedora 17, 18, 19, 20 • Amazon Linux 2012.09 • OS X 10.10 and 10.11
  2. Ϗϧυํ๏ [email protected] how-to-build.md ࢀর # ڞ௨ $ sh build.sh config

    $ sh build.sh build $ sh build.sh test # iOS $ sh build.sh build-ios # watchOS $ sh build.sh build-watchos # tvOS $ sh build.sh build-tvos # Android $ sh build.sh build-android
  3. ϔομϑΝΠϧ [email protected] /// The Table class is non-polymorphic, that is,

    it has no virtual
 /// functions. This is important because it ensures that there is no run-time
 /// distinction between a Table instance and an instance of any variation of
 /// BasicTable<T>, and this, in turn, makes it valid to cast a pointer from
 /// Table to BasicTable<T> even when the instance is constructed as a Table. Of
 /// course, this also assumes that BasicTable<> is non-polymorphic, has no
 /// destructor, and adds no extra data members.
 ///
 /// FIXME: Table assignment (from any group to any group) could be made aliasing
 /// safe as follows: Start by cloning source table into target allocator. On
 /// success, assign, and then deallocate any previous structure at the target.
 ///
 /// FIXME: It might be desirable to have a 'table move' feature between two
 /// places inside the same group (say from a subtable or a mixed column to group
 /// level). This could be done in a very efficient manner.
 ///
 /// FIXME: When compiling in debug mode, all public non-static table functions
 /// should REALM_ASSERT(is_attached()).
 class Table {
 public:
 /// Construct a new freestanding top-level table with static
 /// lifetime.
 ///
 /// This constructor should be used only when placing a table
 /// instance on the stack, and it is then the responsibility of
 /// the application that there are no objects of type TableRef or
 /// ConstTableRef that refer to it, or to any of its subtables,
 /// when it goes out of scope. To create a top-level table with
 /// dynamic lifetime, use Table::create() instead.
 Table(Allocator& = Allocator::get_default());
 
 /// Construct a copy of the specified table as a new freestanding
 /// top-level table with static lifetime.
 ///
 /// This constructor should be used only when placing a table
 /// instance on the stack, and it is then the responsibility of
 /// the application that there are no objects of type TableRef or
 /// ConstTableRef that refer to it, or to any of its subtables,
 /// when it goes out of scope. To create a top-level table with
 /// dynamic lifetime, use Table::copy() instead.
 Table(const Table&, Allocator& = Allocator::get_default());
 ͓קΊϔομϑΝΠϧ group.hpp group_shared.hpp table.hpp string_data.hpp index_string.hpp query_expression.hpp
  4. Realm Java 2.0.0(৽ػೳ) • Realm Mobile PlatformରԠʂʂʂʢϕʔλʣ • άϩʔόϧॳظԽϝιουRealm.init(Context)Λಋೖ •

    ॳظԽϝιουҎ֎Ͱ͸Context͕ෆཁʹ • RealmLogΫϥεʹΑΔϩά੍ޚ • ϞσϧΫϥεͰΦϒδΣΫτͷॳظ஋ΛఆٛՄೳʹ • isManaged()ϝιουΛ௥Ճ [email protected]
  5. Realm Java 2.0.0(ඇޓ׵ͷมߋ1) • ϑΝΠϧͷϑΥʔϚοτ͕มߋʹͳ͍ͬͯ·͢(ࣗಈతʹม׵) • ͸͡ΊʹඞͣRealm.init(Context)ΛݺΜͰ͍ͩ͘͞(Application౳Ͱ) • Ҿ͖਺ʹContextΛऔΔίϯετϥΫλ/ϝιου͸Context͕ͳ͍΋ͷΛ࢖ͬͯͩ͘ ͍͞

    • ΞϯϚωʔδυͳRealmObjectͷisValid()͸ৗʹtrueΛฦ͢Α͏ʹͳΓ·ͨ͠(ଞͷϓ ϥοτϑΥʔϜͱͷޓ׵ੑ) • armeabiͷόΠφϦΛ࡟আ͠·ͨ͠(෮׆ͷՄೳੑ͋Γ #3508) • IncompatibleLockFileException ͱRealmIOExcpetion ͕ഇࢭ͞ΕɺRealmFileException Ͱஔ͖׵͑ΒΕ·ͨ͠ [email protected]
  6. Realm Java 2.0.0(ඇޓ׵ͷมߋ2) • ϓϥΠϚϦΩʔ͕ఆٛ͞ΕͨΫϥεΛRealm.createObject()Ͱ࡞੒͢Δ৔ ߹ɺRealm.createObject(Class)Ͱ͸ͳ͘Realm.createObject(Class,Object) Λ࢖͍ͬͯͩ͘͞ • JSON͔ΒΠϯϙʔτ͢ΔࡍɺϓϥΠϚϦΩʔ͕ఆٛ͞Ε͍ͯΔʹ΋ؔΘ ΒͣJSON

    ্ʹଘࡏ͠ͳ͍৔߹͸ྫ֎͕εϩʔ͞Ε·͢ • ϓϥΠϚϦΩʔͷ஋͸มߋෆՄʹͳΓ·ͨ͠ • Realm.create*FromJson()͸ɺϞσϧΫϥεͷϑΟʔϧυ΍ίϯετϥΫλ Ͱఆٛ͞Εͨॳظ஋Λ൓ө͢ΔΑ͏ʹͳΓ·ͨ͠ [email protected]
  7. Realm Mobile PlatformରԠ [email protected] Credentials creds = Credentials.usernamePassword(username, password, true);


    User.loginAsync(creds, "https://realm.example.com:9443", new User.Callback() {
 @Override
 public void onSuccess(User user) { // ログイン成功時の処理 }
 
 @Override
 public void onError(ObjectServerError error) {
 // エラー処理
 }
 } );
  8. Realm Mobile PlatformରԠ [email protected] // ユーザーとURLから設定を作成
 SyncConfiguration config = new

    SyncConfiguration.Builder(user,
 "realm://realm.example.com/~/userRealm").build();
 
 // 同期されるRealm
 Realm realm = Realm.getInstance(config);
  9. Realm.init(Context)ͷಋೖ [email protected] public class MyApplication extends Application {
 @Override
 public

    void onCreate() {
 super.onCreate();
 
 Realm.init(this);
 
 // その他の初期化
 }
 }
  10. ϞσϧΫϥεͷॳظ஋ [email protected] public class User extends RealmObject {
 @PrimaryKey
 @Required


    public String id = UUID.randomUUID().toString();
 public String name;
 public int age;
 
 public User() {
 this.name = "unknown";
 }
 } String id = UUID.randomUUID().toString();
 User user = realm.createObject(User.class, id);
 print(user.id); // createObjectに渡したidの値
 print(user.name); // "unknown"
  11. ϞσϧΫϥεͷॳظ஋ [email protected] public class Owner extends RealmObject {
 public String

    name;
 public User user = new User();
 } Owner owner = realm.createObject(Owner.class);
 print(owner.user.id); // モデルクラスが生成したランダムなID
  12. ίϯετϥΫλབྷΈͷόάमਖ਼ [email protected] public class User extends RealmObject {
 public String

    name;
 public int age;
 
 public User() {}
 
 public User(User user) {
 this.name = user.name; 
 setAge(20);
 }
 
 public void setAge(int age) {
 this.age = age;
 }
 } NullPointerException user͕Ϛωʔδυͷ৔߹ৗʹ0