使用迁移块

时间:2016-03-31 12:06:43

标签: ios swift realm

我已经向领域对象添加了calculation属性,现在我不得不在迁移块中设置它的值,因为我无法实例化一个对象将其传递给计算器。

这是我的模特:

import Foundation
import RealmSwift

class Loan: Object {

    dynamic var id: String = NSUUID().UUIDString
    dynamic var creationDate: NSDate = NSDate()
    dynamic var title: String = ""
    dynamic var amount: Double = 0.0
    dynamic var rate: Float = 0.0
    dynamic var term: Int = 0
    dynamic var type: Int = TYPE_ANNUITY
    dynamic var firstPaymentDate: NSDate = NSDate()
    dynamic var issueDate: NSDate?

    let extras = List<Extra>() // this is a list of nested realm models which is also used for calculation

    dynamic var calculation: CalculationResult? // !! added this property !!

    static let TYPE_ANNUITY = 0
    static let TYPE_GRADED = 1


    override class func primaryKey() -> String? {
        return LoanPropNames.Id
    }
}

struct LoanPropNames {
    static let CreationDate = "creationDate"
    static let Id = "id"
    static let Calculation = "calculation"
}

calculation的值由具有签名的辅助方法计算,如下所示:

func calculate(loan: Loan) -> CalculationResult

以下是Realm的配置代码:

class RealmConfigurator {

    private static let schemaVersionAddedCalculationProp: UInt64 = 1

    private static let schemaVersionLatest = schemaVersionAddedCalculationProp

    static func createConfiguration() -> Realm.Configuration {

        return Realm.Configuration(
            schemaVersion: schemaVersionLatest,
            migrationBlock: {
                migration, oldSchemaVersion in

                if oldSchemaVersion < schemaVersionAddedCalculationProp {

                    migration.enumerate(Loan.className()) {
                        oldObject, newObject in

                        if let oldObject = oldObject,
                            let newObject = newObject {

                            // this is how I try to instantiate a loan to pass it to the CalculationHelper
                            let loan = Loan(value: oldObject, schema: migration.oldSchema)

                            newObject[LoanPropNames.Calculation] = CalculationHelper.calculate(loan)
                        }    
                    }
                }
            }
        )
    }
}

问题出在

let loan = Loan(value: oldObject, schema: migration.oldSchema)

编译器说

  

无法将'Schema'类型的值转换为预期的参数类型'RLMSchema'

如果我尝试像这样运行它:     let loan = Loan(value:oldObject)

我收到运行时错误:

  

因未捕获的异常“RLMException”而终止应用,原因是:“calculation类的属性名称Loan无效。”

问题:如何在迁移期间实例化贷款对象以填充calculation属性?

我不想将其设为lazy,因为在更改其他属性时需要重新计算它。同时使它成为计算属性并不是一个好的解决方案,因为每次访问需要一段时间来计算值。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用KVC来填充贷款对象的属性。如下所示:

// 1. Create empty loan object
let loan = Loan()

// 2. Fill loan object's properties with old object
let propertyNames = oldObject.objectSchema.properties.map { $0.name }
let values = oldObject.dictionaryWithValuesForKeys(propertyNames)
loan.setValuesForKeysWithDictionary(values)

// 3. Create standalone calcuration object
let calculation = CalculationHelper.calculate(loan)

// 4. Save calculation object and assign
newObject[LoanPropNames.Calculation] = migration.create(CalculationResult.className(), value: calculation)