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

Realm Japan meetup Osaka #1

Realm Japan meetup Osaka #1

Realm Japan meetup Osaka

Kishikawa Katsumi

August 09, 2016
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. Multi platform • Realm Objective-C/Swift 1.0.2 • Realm Java 1.1.1

    (was 1.1.0) • Realm React Native 0.14.0 • Realm Xamarin 0.77.1 (was 0.76.0) [email protected] [email protected]
  2. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } class Comment: Object { dynamic var id: Int = 0 dynamic var content: String = "" dynamic var user: User? = nil } class User: Object { dynamic var id: Int = 0 dynamic var name: String = "" }
  3. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } 0CKFDUך؟ـؙٓأ
  4. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } 0CKFDUך؟ـؙٓأ 㛇劤涸חEZOBNJDWBS
  5. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } ⢽㢩(FOFSJDTך׋׭
  6. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } ؙٓأذ٦ـٕ فٗػذ؍ؕٓي
  7. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } ر٦ة㘗 رؿٕؓز⦼
  8. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } 㼎ךꟼ鸬
  9. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } 㼎㢳ךꟼ鸬
  10. Simple API (Model definition) [email protected] class Article: Object { dynamic

    var id: Int = 0 dynamic var title: String = "" dynamic var content: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var modificationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() override class func primaryKey() -> String? { return "id" } } فٓ؎ؤؗ٦
  11. Usage (Save) [email protected] let realm = try! Realm() let article

    = Article() article.title = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser try! realm.write { in realm.add(article) }
  12. Usage (Save) [email protected] let realm = try! Realm() let article

    = Article() article.title = titleTextfield.text article.contents = contentTextfield.text try! realm.write { in realm.add(article) }
  13. Usage (Save) [email protected] let realm = try! Realm() let article

    = Article() article.title = titleTextfield.text article.contents = contentTextfield.text try! realm.write { in realm.add(article) }
  14. Usage (Save) [email protected] let realm = try! Realm() let article

    = Article() article.title = titleTextfield.text article.contents = contentTextfield.text try! realm.write { in realm.add(article) }
  15. Usage (Save) [email protected] let realm = try! Realm() let article

    = Article() article.title = titleTextfield.text article.contents = contentTextfield.text realm.write { () in realm.add(article) }
  16. Usage (Query) [email protected] let realm = try! Realm() let currentUser

    = realm.objectForPrimaryKey(User.self, key: userID) let articles = realm .objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  17. Usage (Query) [email protected] let realm = try! Realm() let currentUser

    = realm.objectForPrimaryKey(User.self, key: userID) let articles = realm .objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  18. Usage (Query) [email protected] let realm = try! Realm() let currentUser

    = realm.objectForPrimaryKey(User.self, key: userID) let articles = realm .objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  19. Usage (Query) [email protected] let realm = try! Realm() let currentUser

    = realm.objectForPrimaryKey(User.self, key: userID) let articles = realm .objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  20. Usage (Query) [email protected] let realm = try! Realm() let currentUser:

    User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results<Article> = realm .objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  21. Usage (Query) [email protected] let realm = try! Realm() let currentUser:

    User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results<Article> = realm .objects(Article) .filter("user = %@", currentUser) let article: Article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  22. Usage (Query) [email protected] let realm = try! Realm() let currentUser:

    User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results<Article> = realm .objects(Article) .filter("user = %@", currentUser) let article: Article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name
  23. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) let realm = try! Realm(configuration: config)
  24. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) let realm = try! Realm(configuration: config) ⥂㶷㜥䨽ؿ؋؎ٕせ
  25. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) let realm = try! Realm(configuration: config) 农〾⻉
  26. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) let realm = try! Realm(configuration: config) و؎ؚٖ٦ءّٝ儗חؿ؋؎ٕ⵴ꤐ
  27. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) let realm = try! Realm(configuration: config) ֿךؿ؋؎ٕד⢪欽ׅ׷ذ٦ـٕ׾ꣲ㹀
  28. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()
  29. Usage (RealmConfiguration) [email protected] let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: "..."), encryptionKey:

    key, deleteRealmIfMigrationNeeded: true, objectTypes: [Article.self, User.self, Comment.self]) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()
  30. Multi thread [email protected] func updateUnread(id: String) { let realm =

    try! Realm() guard let tweet = realm.objectForPrimaryKey(Article.self, key: id) else { return } request.performRequestWithHandler { (data, response, error) in if let error = error { return } ... try! realm.write { tweet.unread = false } } }
  31. Multi thread [email protected] func updateUnread(id: String) { let realm =

    try! Realm() guard let tweet = realm.objectForPrimaryKey(Article.self, key: id) else { return } request.performRequestWithHandler { (data, response, error) in if let error = error { return } ... try! realm.write { tweet.unread = false } } }
  32. Multi thread [email protected] func updateUnread(id: String) { let realm =

    try! Realm() guard let tweet = realm.objectForPrimaryKey(Article.self, key: id) else { return } request.performRequestWithHandler { (data, response, error) -> Void in if let error = error { return } let realm = try! Realm() if let tweet = realm.objectForPrimaryKey(Tweet.self, key: id) { try! realm.write { tweet.favorited = !tweet.favorited } } } }
  33. Multi thread [email protected] func updateUnread(id: String) { let realm =

    try! Realm() guard let tweet = realm.objectForPrimaryKey(Article.self, key: id) else { return } request.performRequestWithHandler { (data, response, error) -> Void in if let error = error { return } let realm = try! Realm() if let tweet = realm.objectForPrimaryKey(Tweet.self, key: id) { try! realm.write { tweet.favorited = !tweet.favorited } } } }
  34. Best practice [email protected] var timeline: Results<Article>? var notificationToken: NotificationToken? override

    func viewDidLoad() { super.viewDidLoad() ... let realm = try! Realm() timeline = realm.objects(Article.self) .sorted("creationDate", ascending: false) notificationToken = timeline?.addNotificationBlock { [weak self] (results, error) in if let _ = error { return } self?.tableView.reloadData() } } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return timeline?.count ?? 0 } func getTimeline() { ... request.performRequestWithHandler { (data, response, error) -> Void in if let error = error { self.showAlert(error.localizedDescription) return } let articles = try NSJSONSerialization.JSONObjectWithData(data, options: []) let realm = try! Realm() try! realm.write { articles.forEach { (article) -> () in ... realm.add(article, update: true) } } } }
  35. Best practice [email protected] var timeline: Results<Article>? var notificationToken: NotificationToken? override

    func viewDidLoad() { super.viewDidLoad() ... let realm = try! Realm() timeline = realm.objects(Article.self) .sorted("creationDate", ascending: false) notificationToken = timeline?.addNotificationBlock { [weak self] (results, error) in if let _ = error { return } self?.tableView.reloadData() } }
  36. Best practice [email protected] func getTimeline() { ... request.performRequestWithHandler { (data,

    response, error) -> Void in if let error = error { self.showAlert(error.localizedDescription) return } let articles = try NSJSONSerialization.JSONObjectWithData(data, options: []) let realm = try! Realm() try! realm.write { articles.forEach { (article) -> () in ... realm.add(article, update: true) } } } }
  37. Best practice [email protected] func getTimeline() { ... request.performRequestWithHandler { (data,

    response, error) -> Void in if let error = error { self.showAlert(error.localizedDescription) return } let articles = try NSJSONSerialization.JSONObjectWithData(data, options: []) let realm = try! Realm() try! realm.write { articles.forEach { (article) -> () in ... realm.add(article, update: true) } } } } فٓ؎ؤؗ٦׾ⵃ欽׃ג 鷄⸇،حفر٦ز 傀㶷ך3FTVMUTכ荈⹛涸ח 剑倜ךⰻ㺁ח刿倜ׁ׸׷
  38. Migration Tips [email protected] let config = Realm.Configuration(schemaVersion: 1, migrationBlock: {

    (migration, oldSchemaVersion) in migration.enumerate("") { (oldObject, newObject) in ... } }) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()
  39. Migration Tips [email protected] let config = Realm.Configuration(schemaVersion: 1, migrationBlock: {

    (migration, oldSchemaVersion) in migration.enumerate("") { (oldObject, newObject) in ... } }) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()
  40. Migration Tips [email protected] class User: Object { dynamic var id

    = 0 dynamic var name = "" } class User: Object { dynamic var id = 0 dynamic var name = "" dynamic var age 0 }
  41. Migration Tips [email protected] class User: Object { dynamic var id

    = 0 dynamic var firstname = "" dynamic var lastname = "" } class User: Object { dynamic var id = 0 dynamic var fullname = "" }
  42. List<T> is ordered [email protected] class Data: Object { dynamic var

    name = "" ... dynamic var order = 0 // ฒ΂ସ͑ͷͨΊͷΧϥϜ͕ඞཁ }
  43. List<T> is ordered [email protected] override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:

    NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { try! realm.write { let sourceObject = objects[sourceIndexPath.row] let destinationObject = objects[destinationIndexPath.row] let destinationObjectOrder = destinationObject.order if sourceIndexPath.row < destinationIndexPath.row { // ্͔ΒԼʹҠಈͨ͠৔߹ɺؒͷ߲໨Λ্ʹγϑτ for index in sourceIndexPath.row...destinationIndexPath.row { let object = objects[index] object.order -= 1 } } else { // Լ͔Β্ʹҠಈͨ͠৔߹ɺؒͷ߲໨ΛԼʹγϑτ for index in (destinationIndexPath.row..<sourceIndexPath.row).reverse() { let object = objects[index] object.order += 1 } } ɹɹɹɹɹɹɹɹ// Ҡಈͨ͠ηϧͷฒͼΛҠಈઌʹߋ৽ sourceObject.order = destinationObjectOrder } }
  44. List<T> is ordered [email protected] class Article: Object { dynamic var

    name = "" ... dynamic var order = 0 // ฒ΂ସ͑ͷͨΊͷΧϥϜ͕ඞཁ }
  45. List<T> is ordered [email protected] override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:

    NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { try! realm.write { let sourceObject = objects[sourceIndexPath.row] let destinationObject = objects[destinationIndexPath.row] let destinationObjectOrder = destinationObject.order if sourceIndexPath.row < destinationIndexPath.row { // ্͔ΒԼʹҠಈͨ͠৔߹ɺؒͷ߲໨Λ্ʹγϑτ for index in sourceIndexPath.row...destinationIndexPath.row { let object = objects[index] object.order -= 1 } } else { // Լ͔Β্ʹҠಈͨ͠৔߹ɺؒͷ߲໨ΛԼʹγϑτ for index in (destinationIndexPath.row..<sourceIndexPath.row).reverse() { let object = objects[index] object.order += 1 } } ɹɹɹɹɹɹɹɹ// Ҡಈͨ͠ηϧͷฒͼΛҠಈઌʹߋ৽ sourceObject.order = destinationObjectOrder } }
  46. List<T> is ordered [email protected] override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:

    NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { try! realm.write { let sourceObject = objects[sourceIndexPath.row] objects.removeAtIndex(sourceIndexPath.row) objects.insert(sourceObject, atIndex: destinationIndexPath.row) } }
  47. 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] [email protected]