Slide 1

Slide 1 text

Realm meetup Osaka [email protected]

Slide 2

Slide 2 text

#realm_jp [email protected]

Slide 3

Slide 3 text

Katsumi Kishikawa Realm Inc. [email protected]

Slide 4

Slide 4 text

What’s Realm [email protected]

Slide 5

Slide 5 text

What’s Realm • ϞόΠϧσόΠεઐ༻ʹ৽͘͠࡞ΒΕͨσʔλϕʔε • ϞόΠϧ༻్ʹ࠷దԽ͞Ε͍ͯΔ - ஗Ԇϩʔυ - θϩίϐʔɾΞʔΩςΫνϟ - ωΠςΟϒɾϦϯΫ • ಠࣗΤϯδϯʢSQLiteΛ࢖͍ͬͯͳ͍ʣ • Φʔϓϯιʔε [email protected] [email protected]

Slide 6

Slide 6 text

What’s Realm • ߴ଎ͳಈ࡞ • ҉߸ԽΛඪ४૷උ • ϚϧνϓϥοτϑΥʔϜ • ௚ײతͰγϯϓϧͳAPI • ஸೡͳαϙʔτʢ೔ຊޠOKʣ [email protected] [email protected]

Slide 7

Slide 7 text

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]

Slide 8

Slide 8 text

Realm Objective-C/Swift 1.0.2 (was 1.0.1) [email protected]

Slide 9

Slide 9 text

Already compatible with Swift 3 & Xcode 8 https://github.com/realm/realm-cocoa/issues/3796 [email protected]

Slide 10

Slide 10 text

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() } 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 = "" }

Slide 11

Slide 11 text

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() } 0CKFDUך؟ـؙٓأ

Slide 12

Slide 12 text

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() } 0CKFDUך؟ـؙٓأ 㛇劤涸חEZOBNJDWBS

Slide 13

Slide 13 text

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() } ⢽㢩(FOFSJDTך׋׭

Slide 14

Slide 14 text

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() } ؙٓأذ٦ـٕ فٗػذ؍ؕٓي

Slide 15

Slide 15 text

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() } ر٦ة㘗 رؿٕؓز⦼

Slide 16

Slide 16 text

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() } 㼎ךꟼ鸬

Slide 17

Slide 17 text

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() } 㼎㢳ךꟼ鸬

Slide 18

Slide 18 text

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() override class func primaryKey() -> String? { return "id" } } فٓ؎ؤؗ٦

Slide 19

Slide 19 text

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) }

Slide 20

Slide 20 text

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) }

Slide 21

Slide 21 text

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) }

Slide 22

Slide 22 text

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) }

Slide 23

Slide 23 text

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) }

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Usage (Query) [email protected] let realm = try! Realm() let currentUser: User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results = 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

Slide 29

Slide 29 text

Usage (Query) [email protected] let realm = try! Realm() let currentUser: User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results = 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

Slide 30

Slide 30 text

Usage (Query) [email protected] let realm = try! Realm() let currentUser: User = realm.objectForPrimaryKey(User.self, key: userID) let articles: Results = 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

Slide 31

Slide 31 text

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)

Slide 32

Slide 32 text

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) ⥂㶷㜥䨽ؿ؋؎ٕせ

Slide 33

Slide 33 text

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) 农〾⻉

Slide 34

Slide 34 text

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) و؎ؚٖ٦ءّٝ儗חؿ؋؎ٕ⵴ꤐ

Slide 35

Slide 35 text

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) ֿךؿ؋؎ٕד⢪欽ׅ׷ذ٦ـٕ׾ꣲ㹀

Slide 36

Slide 36 text

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()

Slide 37

Slide 37 text

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()

Slide 38

Slide 38 text

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 } } }

Slide 39

Slide 39 text

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 } } }

Slide 40

Slide 40 text

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 } } } }

Slide 41

Slide 41 text

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 } } } }

Slide 42

Slide 42 text

Best practice [email protected] var timeline: Results? 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) } } } }

Slide 43

Slide 43 text

Best practice [email protected] var timeline: Results? 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() } }

Slide 44

Slide 44 text

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) } } } }

Slide 45

Slide 45 text

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כ荈⹛涸ח 剑倜ךⰻ㺁ח刿倜ׁ׸׷

Slide 46

Slide 46 text

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()

Slide 47

Slide 47 text

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()

Slide 48

Slide 48 text

Migration Tips [email protected] let config = Realm.Configuration(schemaVersion: 1) Realm.Configuration.defaultConfiguration = config let realm = try! Realm()

Slide 49

Slide 49 text

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 }

Slide 50

Slide 50 text

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 = "" }

Slide 51

Slide 51 text

Advanced Tips [email protected] • List is ordered

Slide 52

Slide 52 text

List is ordered [email protected] class Data: Object { dynamic var name = "" ... dynamic var order = 0 // ฒ΂ସ͑ͷͨΊͷΧϥϜ͕ඞཁ }

Slide 53

Slide 53 text

List 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..

Slide 54

Slide 54 text

List is ordered [email protected] class Article: Object { dynamic var name = "" ... dynamic var order = 0 // ฒ΂ସ͑ͷͨΊͷΧϥϜ͕ඞཁ }

Slide 55

Slide 55 text

List 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..

Slide 56

Slide 56 text

List is ordered [email protected] class ArticlesWrapper: Object { let articles = List }

Slide 57

Slide 57 text

List 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) } }

Slide 58

Slide 58 text

List is ordered [email protected] class Category: Object { dynamic var name = "" let articles = List }

Slide 59

Slide 59 text

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]

Slide 60

Slide 60 text

Realm Japan User Group Facebook [email protected]

Slide 61

Slide 61 text

Support Chat Slack [email protected]

Slide 62

Slide 62 text

Ξϯέʔτ bit.ly/RealmJP_Kansai [email protected]

Slide 63

Slide 63 text

Questions? Katsuma Kishikawa [email protected] www.realm.io/jp @k_katsumi