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

Realm meetup Japan #22

Realm meetup Japan #22

Realm Objective-C/Swift 2.4-2.7
Realm Functions
Realm events at WWDC

Kishikawa Katsumi

May 30, 2017
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. Recent Updates • Realm Objective-C/Swift 2.7.0 (was 2.4.3) • Realm

    Java 3.3.1 (was 3.0.0) • Realm JS 1.3.1 (was 1.1.1) • Realm .NET 1.4.0 (1.1.1) [email protected]
  2. Permission RealmʗSyncPermission [email protected] let permissionRealm = try! user.permissionRealm() let permissions

    = permissionRealm.objects(SyncPermission.self).filter("...") for permission in permissions { print(permission.path) print(permission.userId) print(permission.mayRead) print(permission.mayWrite) print(permission.mayManage) }
  3. `asyncOpen()`: ඇಉظʹRealmΛ։͘ [email protected] let config = Realm.Configuration(schemaVersion: 9, migrationBlock: {

    migration, oldSchemaVersion in // σʔλྔ͕ଟ͘ɺϚΠάϨʔγϣϯʹ͕͔͔࣌ؒΓͦ͏ͳͱ͖ }) Realm.asyncOpen(configuration: config) { realm, error in if let realm = realm { // ׬ྃͨ͠ΒίʔϧόοΫ͕ݺ͹Εɺ࢖༻ՄೳͳRealmΠϯελϯε͕౉͞ΕΔ } else if let error = error { // Τϥʔ͕ൃੜͨ͠৔߹ } } // ϚΠάϨʔγϣϯ͸όοΫάϥ΢ϯυεϨουͰ࣮ߦ͞ΕΔͷͰɺ׬ྃΛ଴ͨͣʹ࣍ͷॲཧʹਐΊΔ ...
  4. `asyncOpen()`: ඇಉظʹRealmΛ։͘ [email protected] let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL:

    realmURL)) Realm.asyncOpen(configuration: config) { realm, error in if let realm = realm { // ಉظ͞ΕͨRealmͷ৔߹ɺ͢΂ͯͷσʔλ͕μ΢ϯϩʔυ͞Εͨ࣌఺ͰίʔϧόοΫ͕ݺ͹ΕΔ } else if let error = error { // RealmϑΝΠϧͷΦʔϓϯʹࣦഊɺ͋Δ͍͸σʔλͷμ΢ϯϩʔυʹࣦഊ } } ...
  5. `shouldCompactOnLaunch`: ىಈ࣌ʹίϯύΫγϣϯΛ࣮ߦ [email protected] let config = Realm.Configuration(shouldCompactOnLaunch: { totalBytes, usedBytes

    in // totalBytes - ࣮ࡍͷϑΝΠϧ༰ྔʢσʔλྖҬ + ۭ͖ྖҬʣ // usedBytes - ࣮ࡍͷϑΝΠϧ༰ྔͷ͏ͪσʔλྖҬͷαΠζ // totalBytes͕100MBΛ௒͑ɺ͔ۭ͖ͭྖҬ͕50%Ҏ্ͳΒίϯύΫγϣϯΛ࣮ߦ͢Δ let oneHundredMB = 100 * 1024 * 1024 return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5 }) do { // ઃఆͨ͠৚݅ʹ౰ͯ͸·Δ৔߹ɺ࠷ॳʹRealmϑΝΠϧΛ։͘ࡍʹίϯύΫγϣϯ͕࣮ߦ͞ΕΔ let realm = try Realm(configuration: config) } catch { // Τϥʔॲཧ }
  6. Realm Objective-C/Swift 2.7.0 • Reachability APIʹΑΓಉظ͞ΕͨRealmͷ࠶઀ଓ·Ͱͷ࣌ؒΛ࠷ద Խ • Realm Object

    ServerͷRealmೝূͷύεϫʔυมߋ • ಉظ͞ΕͨRealmͷΞΫηεݖΛΑΓ؆୯ʹѻ͑Δ৽͍͠API [email protected]
  7. New Permission APi [email protected] let permissionChange = SyncPermissionChange(realmURL: realmURL, userID:

    anotherUserID, mayRead: true, mayWrite: true, mayManage: false) let managementRealm = try! user.managementRealm() try! managementRealm.write { managementRealm.add(permissionChange) } // Wait for server response token = permissionChange.addNotificationBlock { _ in switch permissionChange.status { case .notProcessed: break // handle case case .success: break // handle case case .error: break // handle case } print(permissionChange.statusMessage) // contains error or informational message } Before:
  8. New Permission APi [email protected] let permission = SyncPermissionValue(realmPath: realmPath, userID:

    anotherUserID, accessLevel: .write) user.applyPermission(permission) { error in if let error = error { // handle error return } // success! } After:
  9. New Permission APi [email protected] // Retrieving permissions user.retrievePermissions { permissions,

    error in if let error = error { // handle error return } // success! access permissions } After:
  10. Realm Functions • αʔόʔαΠυͷεΫϦϓτ࣮ߦ؀ڥ • Realm Mobile PlatformͷWebμογϡϘʔυ͔ΒRealmͷมߋΛτϦΨʔʹ࣮ ߦ͞ΕΔ೚ҙͷίʔυΛهड़Ͱ͖Δ •

    Realm Mobile PlatformͷશΤσΟγϣϯͰར༻Մೳ • ※Developer Edition͸̏ͭ·Ͱͷ੍ݶ͋Γ • ϕʔλ൛ • ࣮ߦ࣌ؒɾίʔυαΠζͳͲDeveloper Editionʹ͓͚Δొ࿥਺Ҏ֎ͷ੍ݶͳ ͠ʢࠓͷͱ͜Ζʣ • ೚ҙͷ֎෦API΍Node.jsϥΠϒϥϦΛ࢖༻Մೳ [email protected]
  11. Realm Functions [email protected] console.log("Starting function"); // add your initialization code

    here module.exports = function(changeEvent) { // event handler code goes here console.log("Changes in realm at:", changeEvent.path); var realm = changeEvent.realm; for (var className in changeEvent.changes) { var changes = changeEvent.changes[className]; var objects = realm.objects(className); ... } console.log(""); };
  12. Realm Functions [email protected] var i, pos; for (i = 0;

    i < changes.insertions.length; i++) { pos = changes.insertions[i]; var inserted = objects[pos] ... } for (i = 0; i < changes.modifications.length; i++) { pos = changes.modifications[i]; var modified = objects[pos] ... } for (i = 0; i < changes.deletions.length; i++) { pos = changes.deletions[i]; var deleted = objects[pos] ... }