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

Realm Japan meetup Ueda

zaki50
November 12, 2016

Realm Japan meetup Ueda

zaki50

November 12, 2016
Tweet

More Decks by zaki50

Other Decks in Technology

Transcript

  1. Realm ˖ ؙٗأفٓحزؿؓ٦ي ˖ J04"OESPJE ˖ +BWB 4XJGU 0CKFDUJWF$ $

    +BWB4DSJQU ˖ ؔ٦فٝا٦أ ˖ 杝荈%#ؒٝآٝ42-JUF׾⢪׏גְזְ [email protected]
  2. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ [email protected]
  3. class Article: Object { dynamic var id: Int = 0

    dynamic var title: String = "" dynamic var contents: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var modificationDate: NSDate = NSDate() dynamic var user: User? = nil let comments = List<Comment>() } Ϟσϧʢςʔϒϧʣఆٛ (iOS) [email protected]
  4. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ RealmObjectΫϥεΛܧঝ [email protected]
  5. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ ΧϥϜͱσʔλܕ [email protected]
  6. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ nullͷՄʗෆՄ [email protected]
  7. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ ̍ର̍ͷؔ࿈ [email protected]
  8. public class Article extends RealmObject { @PrimaryKey private int id;

    @Required private String title; private String contents; private Date creationDate; private Date modificationDate; private User user; private RealmList<Comment> comments; // getter, setter } Ϟσϧʢςʔϒϧʣఆٛ ̍ରଟͷؔ࿈ [email protected]
  9. byte/Byte short/Short int/Integer long/Long boolean/Boolean float/Float double/Double String Date byte[]

    モデルクラス RealmList<モデルクラス> Ϟσϧʢςʔϒϧʣఆٛ ΧϥϜʹ࢖༻Ͱ͖Δܕ [email protected]
  10. Article article = new Article(); article.setId(1);
 article.setTitle("Realm meetup");
 
 Realm

    realm = Realm.getDefaultInstance();
 try {
 realm.executeTransaction(realm -> { realm.insert(article);
 });
 } finally {
 realm.close();
 } σʔλͷอଘ [email protected]
  11. Article article = new Article(); article.setId(1);
 article.setTitle("Realm meetup");
 
 Realm

    realm = Realm.getDefaultInstance();
 try {
 realm.executeTransaction(realm -> { realm.insert(person);
 });
 } finally {
 realm.close();
 } σʔλͷอଘ ϞσϧΦϒδΣΫτͷ࡞੒ [email protected]
  12. Article article = new Article(); article.setId(1);
 article.setTitle("Realm meetup");
 
 Realm

    realm = Realm.getDefaultInstance();
 try {
 realm.executeTransaction(realm -> { realm.insert(person);
 });
 } finally {
 realm.close();
 } σʔλͷอଘ RealmΠϯελϯεͷऔಘ [email protected]
  13. Article article = new Article(); article.setId(1);
 article.setTitle("Realm meetup");
 
 Realm

    realm = Realm.getDefaultInstance();
 try {
 realm.executeTransaction(realm -> { realm.insert(person);
 });
 } finally {
 realm.close();
 } σʔλͷอଘ ςʔϒϧ΁ૠೖ [email protected]
  14. Realm realm = Realm.getDefaultInstance();
 try { RealmResults<Article> articles = realm.where(Article.class)

    .beginsWith("title", "Realm") .findAllSorted("creationDate", Sort.DESCENDING); } finally {
 realm.close();
 } ΫΤϦ [email protected]
  15. Realm realm = Realm.getDefaultInstance();
 try { RealmResults<Article> articles = realm.where(Article.class)

    .beginsWith("title", "Realm") .findAllSorted("creationDate", Sort.DESCENDING); } finally {
 realm.close();
 } ΫΤϦ RealmΠϯελϯεͷऔಘ [email protected]
  16. Realm realm = Realm.getDefaultInstance();
 try { RealmResults<Article> articles = realm.where(Article.class)

    .beginsWith("title", "Realm") .findAllSorted("creationDate", Sort.DESCENDING); } finally {
 realm.close();
 } ΫΤϦ ৚݅ͷࢦఆͱ݁Ռͷऔಘ [email protected]
  17. Realm realm = Realm.getDefaultInstance();
 try { RealmResults<Article> articles = realm.where(Article.class)

    .equalTo("user.name", "Tim") .lessThan("user.age", 30) .findAllSorted("creationDate", Sort.DESCENDING); } finally {
 realm.close();
 } ϦϯΫΫΤϦ [email protected]
  18. Realm realm = Realm.getDefaultInstance();
 try { RealmResults<Article> articles = realm.where(Article.class)

    .contains("comments.message", "Ueda") .contains("comments.message", "meetup") .findAllSorted("creationDate", Sort.DESCENDING); } finally {
 realm.close();
 } ϦϯΫΫΤϦ ஫ҙʂ [email protected]
  19. Realm realm = Realm.getDefaultInstance();
 try {
 RealmResults<Article> articles = ...;

    Article article = articles.first(); article.isManaged(); // true Log.d("realm", article.getTitle()); Log.d("realm", article.getUser().getName()); Log.d("realm", article.getComments().get(0).getMessage()); } finally {
 realm.close();
 } σʔλͷಡΈग़͠ [email protected]
  20. RealmResults<Article> articles = ...; articles.addChangeListener(results -> { // ArticleΫϥεʹ͍ͭͯมߋɾ௥Ճɾ࡟আͷࡍʹݺ͹ΕΔ });

    Article article = articles.first(); article.addChangeListener(obj -> { // ArticleΫϥεʹ͍ͭͯมߋɾ௥Ճɾ࡟আͷࡍʹݺ͹ΕΔ }); มߋ௨஌ [email protected]
  21. ࣮ࡍͷ࢖͍ํ Activityฤ public class ArticleActivity extends AppCompatActivity {
 private Realm

    realm;
 private Article article;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_article);
 int id = getIntent().getIntExtra("id", 0);
 realm = Realm.getDefaultInstance(); article = realm.where(Article.class).equalTo("id", id);
 } @Override
 public void onDestroy() {
 super.onDestroy(); realm.close(); } } [email protected]
  22. ࣮ࡍͷ࢖͍ํ Fragmentฤ public class ArticleFragment extends Fragment {
 private Realm

    realm;
 private Article article;
 @Override
 public View onCreateView(...) { super.onCreateView(...); realm = Realm.getDefaultInstance(); int id = getIntent().getIntExtra("id", 0);
 article = realm.where(Article.class).equalTo("id", id); // Viewの作成
 } @Override
 public void onDestroyView() {
 super.onDestroyView(); realm.close(); } } [email protected]
  23. ࣮ࡍͷ࢖͍ํ ඇLooperεϨουฤ public class MyThread extends Thread {
 @Override
 public

    void run() { while(true) { // なんらかの処理 //結果を保存 Realm realm = Realm.getDefaultInstance(); try {
 realm.executeTransaction(realm -> { realm.insert(person);
 });
 } finally {
 realm.close();
 } } } } [email protected]
  24. ࣮ࡍͷ࢖͍ํ ඇLooperεϨουฤ public class MyThread extends Thread {
 @Override
 public

    void run() { Realm realm = Realm.getDefaultInstance(); try { RealmResults<Article> articles = ...;
 while(true) { realm.waitForChange(); //articlesを読み出して必要な処理を実行 } } finally {
 realm.close();
 } } } [email protected]
  25. public class MyApplication extends Application {
 @Override
 public void onCreate()

    {
 super.onCreate(); 
 Realm.init(this);
 }
 }
 શମͷॳظԽ [email protected]
  26. unmanaged/managed Article unmanagedArticle = new Article(); unmanagedArticle.isManaged(); // false unmanagedArticle.setTitle("meetup");

    // OK RealmResults<Article> articles = ...; final Article managedArticle = articles.first(); managedArticle.isManaged(); // true managedArticle.setTitle("meetup"); // τϥϯβΫγϣϯΤϥʔ realm.executeTransaction(realm -> { managedArticle.setTitle("meetup"); // OK });
 [email protected]
  27. byte[] key = new byte[64];
 new SecureRandom().nextBytes(key);
 final RealmConfiguration config

    = new RealmConfiguration.Builder()
 .encryptionKey(key)
 .build();
 
 final Realm realm = Realm.getInstance(config);
 ҉߸Խ [email protected]
  28. byte[] key = new byte[64];
 new SecureRandom().nextBytes(key);
 final RealmConfiguration config

    = new RealmConfiguration.Builder()
 .encryptionKey(key)
 .build();
 
 final Realm realm = Realm.getInstance(config);
 ҉߸Խ 伴͸64όΠτ [email protected]
  29. byte[] key = new byte[64];
 new SecureRandom().nextBytes(key);
 final RealmConfiguration config

    = new RealmConfiguration.Builder()
 .encryptionKey(key)
 .build();
 
 final Realm realm = Realm.getInstance(config);
 ҉߸Խ 伴ͷੜ੒ͷҰྫ [email protected]
  30. byte[] key = new byte[64];
 new SecureRandom().nextBytes(key);
 final RealmConfiguration config

    = new RealmConfiguration.Builder()
 .encryptionKey(key)
 .build();
 
 final Realm realm = Realm.getInstance(config);
 ҉߸Խ 伴Ληοτͨ͠ઃఆ৘ใͷ࡞੒ [email protected]
  31. byte[] key = new byte[64];
 new SecureRandom().nextBytes(key);
 final RealmConfiguration config

    = new RealmConfiguration.Builder()
 .encryptionKey(key)
 .build();
 
 final Realm realm = Realm.getInstance(config);
 ҉߸Խ RealmΠϯελϯεͷऔಘ [email protected]
  32. Realm Mobile Platform Realm Mobile Database Data Sync Access Control

    User Identify ٔ،ٕة؎يず劍 ِ٦ؠ٦陎ⴽ ر٦ةⰟ剣 [email protected]
  33. Realm Mobile Platform Realm Mobile Database Data Sync Access Control

    User Identify Event Handling ٔ،ٕة؎يず劍 ِ٦ؠ٦陎ⴽ ر٦ةⰟ剣 ؽآطأٗآحؙ [email protected]
  34. جຊతͳ࢖͍ํ(Android) ೝূ Credentials creds = Credentials.usernamePassword(username, password, true);
 User.loginAsync(creds, "https://realm.example.com:9443/auth",

    new User.Callback() {
 @Override
 public void onSuccess(User user) { // ログイン成功時の処理 }
 
 @Override
 public void onError(ObjectServerError error) {
 // エラー処理
 }
 } ); [email protected]
  35. جຊతͳ࢖͍ํ(iOS) User.authenticate(with: Credential.usernamePassword(username: "username", password: "password", actions: []), server: URL(string:

    "http://128.199.141.202:9080")!, onCompletion: { user, error in guard let user = user else { fatalError(String(describing: error)) } let configuration = Realm.Configuration( syncConfiguration: (user, URL(string: "realm://128.199.141.202:9080/~/realmtasks")!) ) self.realm = try! Realm(configuration: configuration) }) [email protected]
  36. FacebookϩάΠϯ facebookAuth = new FacebookAuth(loginButton) {
 @Override
 public void onRegistrationComplete(LoginResult

    loginResult) {
 SyncCredentials credentials = SyncCredentials.facebook( loginResult.getAccessToken().getToken());
 SyncUser.loginAsync(credentials, AUTH_URL, LoginActivity.this);
 }
 };
 [email protected]
  37. Typical Application Data Flow Native object JSON Native object SQL

    Native object JSON Native object SQLite/CoreData SQLite/CoreData [email protected]
  38. ΠϕϯτϋϯυϦϯά var Realm = require('realm'); var filter_callback = function(realm_name) {

    ... }; var change_notification_callback = function(realm_name, realm) { var channels = realm.objects('Channel'); for (var i = 0; i < channels.length; ++i) { var channel = channels[i]; ... } }; Realm.setGlobalSyncListener(local_root_dir, server_base_url, access_token, filter_callback, change_notification_callback); [email protected]
  39. Realm Mobile Platform ˖ .PCJMF%BUBCBTF ˖ ⿽倯ぢךر٦ةず劍 ˖ ٔ،ٕة؎ي ˖

    ء٦يٖأ ˖ ؝ٝؿؙٔزך荈⹛鍑寸 ˖ ِ٦ؠ٦陎ⴽ ˖ ؎كٝزعٝسؚٔٝ • αʔόʔϓογϡ • σʔλڞ༗ • ΞΫηείϯτϩʔϧ [email protected]
  40. Where to find us • $ Realm Japan User Group:

    facebook.com/groups/realmjp • $ Twitter: twitter.com/realmJapan • GitHub: github.com/realm • $ StackOverflow: ja.stackoverflow.com/questions/tagged/realm • $ Email: [email protected] • $ Slack: slack.realm.io/ [email protected]