Created
August 3, 2018 15:32
-
-
Save dimroc/49cc4384eaaa037844443b1f5b06096d to your computer and use it in GitHub Desktop.
Lightweight management of Realm migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RealmConfiguration+extensions.swift | |
// CrowdCount | |
// | |
// Created by Dimitri Roche on 8/3/18. | |
// inspired by https://medium.com/@shenghuawu/realm-lightweight-migration-4559b9920487 | |
// Copyright © 2018 Dimitri Roche. All rights reserved. | |
// | |
import Foundation | |
import RealmSwift | |
extension Realm.Configuration { | |
typealias ShortMigrationBlock = (RealmSwift.Migration) -> Void | |
static var crowdCountConfiguration: Realm.Configuration { | |
return Realm.Configuration( | |
schemaVersion: UInt64(migrations.count), | |
migrationBlock: { | |
let migration = $0 | |
let current = Int($1) | |
migrations[current..<migrations.count] | |
.forEach { $0(migration) } | |
}) | |
} | |
fileprivate static let migrations: [ShortMigrationBlock] = [ | |
{ migration in | |
migration.enumerateObjects(ofType: PredictionModel.className()) { (_, new) in | |
new?["id"] = UUID().uuidString | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment