Skip to content

Instantly share code, notes, and snippets.

@ppaulojr ppaulojr/realm.md
Created Jan 17, 2017

Embed
What would you like to do?

Why it seemed that Realm was slower

Actually Realm is much faster than Core Data

Instrumenting the code

- (void)createOrUpdateStaffRoundablesWithResponse:(NSArray<CHResponseRoundableMemberStaff *> *)response
{
    TICK(FOR_TIME);
    for (CHResponseRoundableMemberStaff *staffResponse in response) {
        RealmStaff *staff = [RealmStaff createOrUpdateWithResponse:staffResponse];
        staff.careProvider = self;
    }
    TOCK(FOR_TIME);
    
    TICK(ARRAY_TIME);
    NSArray *ids = [response bk_map:^NSString *(CHResponseRoundableMemberStaff *staffResponse) {
        return staffResponse.uid;
    }];
    TOCK(ARRAY_TIME);
    
    TICK(REALM_QUERY_DELETE_TIME);
    RLMResults *results = [RealmStaff objectsWithPredicate:[NSPredicate predicateWithFormat:@"NOT uid IN %@", ids]];
    TOCK(REALM_QUERY_DELETE_TIME);

    TICK(REALM_DELETE_TIME);
    [results.realm deleteObjects:results];
    TOCK(REALM_DELETE_TIME);
}

Results:

2017-01-17 15:59:54.884 Orchid Staging[32243:3865356] FOR_TIME: 1.508549
2017-01-17 15:59:54.898 Orchid Staging[32243:3865356] ARRAY_TIME: 0.013498
2017-01-17 15:59:55.011 Orchid Staging[32243:3865356] REALM_QUERY_DELETE_TIME: 0.112714
2017-01-17 16:01:45.481 Orchid Staging[32243:3865356] REALM_DELETE_TIME: 110.469327
2017-01-17 16:01:45.481 Orchid Staging[32243:3865356] TOTAL_REALM_TIME: 112.108052

Why delete is slow?

The answer seems to be on the way we are using predicates with Realm.

The predicate NOT IN in Realm when we use the data from it seems to be painfully slower than the predicate IN.

In the linked question there are some alternatives.

@ppaulojr

This comment has been minimized.

Copy link
Owner Author

commented Jan 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.