我正在努力将Core Data实体迁移到Realm。实体中可能有超过200万条记录,所以我希望尽可能高效地完成这项工作。
迁移分批处理10000条记录,以下是我用来执行此操作的代码:
/// We're in a loop
/// Get the 10000 records out of Core Data
[realm beginWriteTransaction];
// samples is an NSArray of NSManagedObjects
NSInteger numberOfSamples = samples.count;
for (NSInteger i = 0; i < numberOfSamples; i++)
{
// WeightSample is an NSManagedObject
WeightSample *sample = samples[i];
//Diagnostic is an RLMObject
Diagnostic *weightSample = [Diagnostic new];
///
/// transfer data from WeightSample to Diagnostic
///
[realm addObject:weightSample];
// Remove the old sample from Core Data
[context deleteObject:sample];
}
[realm commitWriteTransaction];
/// Start over at the top of the loop
[context deleteObject:sample]
区块内遇到beginWriteTransaction-commitWriteTransaction
的任何问题?根据我神秘的tweet @Realm和他们的回复,我的假设是“不,但我会阻止更长时间”。
答案 0 :(得分:1)
我做过类似于Realm的事情,但是,在我的情况下,我发现在迁移完成后删除所有核心数据sqlite文件,最快/最有效。我假设迁移完成后你不需要你的核心数据存储,所以,我就是这样做的。
/// We're in a loop
/// Get the 10000 records out of Core Data
[realm beginWriteTransaction];
// samples is an NSArray of NSManagedObjects
// WeightSample is an NSManagedObject
for (WeightSample *sample in samples)
{
//Diagnostic is an RLMObject
Diagnostic *weightSample = [Diagnostic new];
///
/// transfer data from WeightSample to Diagnostic
///
[realm addObject:weightSample];
}
[realm commitWriteTransaction];
/// Start over at the top of the loop
/// After all the data has been transferred to realm, delete all core-data sqlite files using NSFileManager API.