具有大数据库的领域迁移

时间:2020-07-31 11:08:08

标签: database migration realm

我需要通过处理大量数据(超过2,000,000条记录)来实现迁移。当我检查迁移时,内存线性增长到2.4 GB(iPad Pro 12.9 1 gen),应用程序崩溃。如何将迁移分为几个步骤,以便不占用太多内存?我试图将Blob数据转换为多个领域模型。

之前:

@interface RealmDataModel : RLMObject

@property NSData *data; // The data stored in JSON format with different types

@end

之后:

@interface RealmDataModel : RLMObject

@property RLMArray<RealmNew1Model *>< RealmNew1Model > *type1;
@property RLMArray<RealmNew2Model *>< RealmNew2Model > *type2;
@property RLMArray<RealmNew3Model *>< RealmNew3Model > *type3;

@end

迁移:

[migration enumerateObjects:@"RealmDataModel" block:^(RLMObject *oldObject, RLMObject *newObject) {  
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:oldObject.data options:NSJSONReadingAllowFragments error:error];
    for (NSDictionary *someObject in dataArray) {
        if (someObject[@"type"] == @(1)) {
            RLMObject *new1Object = [migration createObject:@"RealmNew1Model" withValue:@{}];
            // convert someObject to the RealmNew1Model
        } else if (someObject[@"type"] == @(2)) {
            RLMObject *new2Object = [migration createObject:@"RealmNew2Model" withValue:@{}];
            // convert someObject to the RealmNew2Model
        } else if (someObject[@"type"] == @(3)) {
            RLMObject *new3Object = [migration createObject:@"RealmNew3Model" withValue:@{}];
            // convert someObject to the RealmNew3Model
        }
    }
}];

0 个答案:

没有答案