删除领域写入事务块中的核心数据对象?

Deleting a Core Data object within a Realm Write Transaction Block?

我正在努力将核心数据实体迁移到 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

根据我的神秘 tweet @Realm 和他们的回复,我的假设是“不,但我会阻止更长时间”。

我在 Realm 中做过类似的事情,但是,在我的例子中,我发现在迁移完成后只删除所有核心数据 sqlite 文件,fastest/most 效率很高。我假设您在迁移完成后不需要您的核心数据存储,所以,这就是我的做法。

/// 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.