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

Realm Japan meetup #15

Realm Japan meetup #15

- Releases Realm 1.0!
- Fine-grained Notifications
- Linking objects properties
- Realm quiz

Kishikawa Katsumi

July 24, 2016
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. 'JOFHSBJOFE/PUJDBUJPO let dogs = realm.objects(Dog) token = dogs.addNotificationBlock { (change:

    RealmCollectionChange) in switch change { case let .Initial(change): ... case let .Update(change, deletions, insertions, modifications): ... case let .Error(error): ... } }
  2. token = dogs.addNotificationBlock { (change: RealmCollectionChange) in switch change {

    case let .Initial(change): self.tableView.reloadData() case let .Update(change, deletions, insertions, modifications): self.tableView .beginUpdates() self.tableView .deleteRowsAtIndexPaths( deletions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView .insertRowsAtIndexPaths( insertions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView.endUpdates() ...
  3. token = dogs.addNotificationBlock { (change: RealmCollectionChange) in switch change {

    case let .Initial(change): self.tableView.reloadData() case let .Update(change, deletions, insertions, modifications): self.tableView .beginUpdates() self.tableView .deleteRowsAtIndexPaths( deletions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView .insertRowsAtIndexPaths( insertions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView.endUpdates() ...
  4. token = dogs.addNotificationBlock { (change: RealmCollectionChange) in switch change {

    case let .Initial(change): self.tableView.reloadData() case let .Update(change, deletions, insertions, modifications): self.tableView .beginUpdates() self.tableView .deleteRowsAtIndexPaths( deletions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView .insertRowsAtIndexPaths( insertions.map { NSIndexPath(forRow: $0, inSection: 0) }, withRowAnimation: .Automatic) self.tableView.endUpdates() ...
  5. -JOLJOHPCKFDUTQSPQFSUJFT class Person: Object { dynamic var name = ""

    dynamic var age = 0 let dogs = List<Dog>() } class Dog: Object { dynamic var name = "" var owners: [Person] { return linkingObjects(Person.self, forProperty: "dogs") } }
  6. -JOLJOHPCKFDUTQSPQFSUJFT class Person: Object { dynamic var name = ""

    dynamic var age = 0 let dogs = List<Dog>() } class Dog: Object { dynamic var name = "" var owners: [Person] { return linkingObjects(Person.self, forProperty: "dogs") } }
  7. -JOLJOHPCKFDUTQSPQFSUJFT class Person: Object { dynamic var name = ""

    dynamic var age = 0 let dogs = List<Dog>() } class Dog: Object { dynamic var name = "" let owners = LinkingObjects(fromType: Person.self, property: "dogs") }
  8. -JOLJOHPCKFDUTQSPQFSUJFT class Person: Object { dynamic var name = ""

    dynamic var age = 0 let dogs = List<Dog>() } class Dog: Object { dynamic var name = "" let owners = LinkingObjects(fromType: Person.self, property: "dogs") }
  9. /4%BUFOBOPTFDPOETTVQQPSU let results = realm.objects(Article) let article = results.first! let

    dateFormatter = NSDateFormatter() dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! dateFormatter.dateFormat = “yyyy-MM-dd'T'HH:mm:ss.SSS" print(dateFormatter.stringFromDate(article.creationDate)) // => 2016-05-27T16:28:38.172 let component = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! .components([.Year, .Month, .Day, .Hour, .Minute, .Second, .Nanosecond], fromDate: article.creationDate) print(“\(c.year)-\(c.month)-\(c.day)T\(c.hour):\(c.minute):\(c.second).\(c.nanosecond)”) // => 2016-5-27T16:28:38.172492027
  10. ؙؒٔךػؿؓ٦وٝأ let articles = realm .objects(Article) ו׍׵ָ鸞ְד׃׳ֲ let recentArticles =

    realm.objects(Article) .filter("modificationDate > ...") .filter("deletionDate != nil") .sorted("modificationDate", ascending: false)
  11. ؙؒٔךػؿؓ٦وٝأחאְג ו׍׵׮ֿך儗挿דכ嗚稊勴⟝׾⥂䭯׃גְ׷׌ֽկ 㹋ꥷךؙؒٔכ㹋遤ׁ׸זְկ let articles = realm .objects(Article) let recentArticles

    = realm.objects(Article) .filter("modificationDate > ...") .filter("deletionDate != nil") .sorted("modificationDate", ascending: false)
  12. ؙؒٔךػؿؓ٦وٝأחאְג ו׍׵׮ֿך儗挿דכ嗚稊勴⟝׾⥂䭯׃גְ׷׌ֽկ 㹋ꥷךؙؒٔכ㹋遤ׁ׸זְկ let articles = realm .objects(Article) return articles.count

    let recentArticles = realm.objects(Article) .filter("modificationDate > ...") .filter("deletionDate != nil") .sorted("modificationDate", ascending: false) recentArticles[indexPath.row]
  13. ׮׃ָؙؒٔ鹼ְ㜥さכ غحؙؚٓؐٝسؙؒٔ׾ⵃ欽ׅ׷ notificationToken = realm.objects(Article) .filter("modificationDate > ...") .filter("deletionDate !=

    nil") .sorted("modificationDate", ascending: false) .addNotificationBlock { (changes: RealmCollectionChange) in switch changes { case let .Initial(results): ... case let .Update(results, deletions, insertions, modifications): ... case let .Error(error): ... } } ؙؒٔכغحؙؚٓؐٝسד㹋遤ׁ׸ծ 㸣✪׃׋儗挿ד؝٦ٕغحָؙㄎל׸׷
  14. و؎ؚٖ٦ءّٝ و؎ؚٖ٦ءّٝכ䗳銲 class User: Object { dynamic var id =

    0 dynamic var name = "" } class User: Object { dynamic var id = 0 dynamic var name = "" dynamic var age 0 }
  15. و؎ؚٖ٦ءّٝ و؎ؚٖ٦ءّٝכ䗳銲 class Article: Object {...} class Comment: Object {...}

    class User: Object {...} class Article: Object {...} class Comment: Object {...} class User: Object {...} class News: Object {...}
  16. و؎ؚٖ٦ءّٝ و؎ؚٖ٦ءّٝכ䗳銲 class User: Object { dynamic var id =

    0 dynamic var name = “" } class User: Object { dynamic var id = 0 dynamic var name = "" class override func indexedProperties() -> [String] { return ["name"] } }