领域;在将来的迁移中创建一个可选属性

时间:2016-11-10 10:47:28

标签: swift realm

我有一个Realm模型,它有一个可选的NA属性TEXT <- c('Learned a new concept today : metamorphic testing. t.co/0is1IUs3aW','BMC Bioinformatics BioMed Central: Detecting novel ncRNAs by experimental #RNomics is not an easy task... http:/t.co/ui3Unxpx #bing @MyEN','BMC Bioinformatics BioMed Central: small #RNA with a regulatory function as a scientific ... Detecting novel… http:/t.co/wWHOEkR0vc #bing','True or false? link(#Addition, #Classification) http:/t.co/zMJuTFt8iq #Oxytocin','Biologists do have a sense of humor, especially computational bio people http:/t.co/wFZqaaFy') NAME <- c('QSoft Consulting','Fabrice Leclerc','Sungsam Gong','Frederic','Zach Stednick') SCREEN_NAME <-c ('QSoftConsulting','rnomics','sunggong','rnomics','jdwasmuth') FOLLOWERS_COUNT <- c(734,1900,234,266,788) RETWEET <- c(1,3,5,0,2) FRIENDS_COUNT <-c(34,532,77,213,422) STATUSES_COUNT <- c(234,643,899,222,226) FAVOURITES_COUNT <- c(144,2677,445,930,254) df <- data.frame(TEXT,NAME,SCREEN_NAME,RETWEET,FRIENDS_COUNT,STATUSES_COUNT,FAVOURITES_COUNT) mydata<-df mydata$FAVOURITES_COUNT <- ifelse( mydata$FAVOURITES_COUNT >= 445, 1, 0) #converting fav_count to binary values ,它存储了对象在之后不再可用的日期。

需要更改此属性,以便library(caret) split=0.60 trainIndex <- createDataPartition(mydata$FAVOURITES_COUNT, p=split, list=FALSE) data_train <- mydata[ trainIndex,] data_test <- mydata[-trainIndex,] 是必需的。因为我们的应用程序有多个版本,我想避免在我们的数据层中有一个函数来检查是否设置了每个对象上的library(e1071) model1 <- glm(FAVOURITES_COUNT~.,family = binomial, data = data_train) summary(model1) 属性,如果没有,则将其设置为默认值。

我计划更新我们的架构以取代

NSDate

expiryDate

此更改是否意味着所有先前存储的未设置expiryDate的对象将设置为expiryDate并将相应地保存到Realm?或者每次访问该属性时都会重新计算该属性吗?

1 个答案:

答案 0 :(得分:1)

可以在Realm迁移期间更改值类型,包括使给定属性可选或不可选。在迁移过程中,您必须经历并为之前为expiryDate的每个nil对象分配值。

Realm.Configuration.defaultConfiguration = Realm.Configuration(
    schemaVersion: 1,
    migrationBlock: { migration, oldSchemaVersion in
        if (oldSchemaVersion < 1) {
            migration.enumerateObjects(ofType: Object.className()) { oldObject, newObject in
                if oldObject["expiryDate"] == nil {
                    newObject["expiryDate"] = Date().dateByAddingSeconds(SomeMaxExpiryDateInSeconds)
                }
            }
        }
    })