Realm-cocoa: Unable to migrate objects that are deleted from schema

Created on 3 Sep 2020  路  3Comments  路  Source: realm/realm-cocoa

Goals

I want to enumerate objects that are deleted from schema in migration block so that I could move information from those objects to separate place.

Expected Results

RLMMigration.enumerateObjects block to be called with valid oldObject and newObject is null.

Actual Results

RLMMigration.enumerateObjects block is not called at all for object types that are deleted from schema.

Probable cause

Update to latest object store in Realm 5.0 seemed to change condition when migration block is called. Previously block was called if queried object type was available only in old schema but current implementation seems to require that object type exists in both old and new schema.

Link to the commit that might have caused the issue:
https://github.com/realm/realm-cocoa/commit/982c0780aa307e3410264f77983e3115c20c795c

Version of Realm and Tooling


Realm framework version: 5.3.6
Realm Object Server version: -
Xcode version: 11.6
iOS version: 13.6
Dependency manager + version: -

O-Community T-Bug

All 3 comments

Hi @tomipu ,
Could you please share a Realm configuration?

@pavel-ship-it
Here's my configuration. What I did in version 91 was that I removed Object10.class from config.objectClasses list and deleted class from project.

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

// Define schema objects
config.objectClasses = @[Object1.class,
                         Object2.class,
                         ...
                         Object9.class];

// Compact if the database file is over 100MB in size and less than 50% 'used'
config.shouldCompactOnLaunch = ^BOOL(NSUInteger totalBytes, NSUInteger usedBytes) {
    NSUInteger oneHundredMB = 100 * 1024 * 1024;
    return (totalBytes > oneHundredMB) && ((double)usedBytes / totalBytes) < 0.5;
};

// Migration
config.schemaVersion = 91; // Delete Object10 from schema
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    ...
    if (oldSchemaVersion < 91) {
        // Copy data from deleted object to file storage
        [migration enumerateObjects:@"Object10" block:^(RLMObject * _Nullable oldObject, RLMObject * _Nullable newObject) {
            NSString *objectID = oldObject[@"id"];
            NSData *data = oldObject[@"data"];
            if (objectID && data) {
                [self saveData:data toFileStorageID:objectID];
            }
        }];
    }
};

[RLMRealmConfiguration setDefaultConfiguration:config];

Using debugger I see that Realm's enumerateObjects founds old objects from old schema just fine but it doesn't execute user defined block because of this line (copied from RLMMigration.mm:95):

    if (oldObjects.count == 0 || objects.count == 0) {  
         return;
     }

Because new schema doesn't have deleted object, objects.count is zero and method returns early.

The same issue happens to me, I want to "rename" an object, and the migration block is not executed.
Any news @pavel-ship-it ?

Was this page helpful?
0 / 5 - 0 ratings