Skip to content

Instantly share code, notes, and snippets.

@ppaulojr
Created January 17, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppaulojr/94d5695469cb6a100ff708e4476e283c to your computer and use it in GitHub Desktop.
Save ppaulojr/94d5695469cb6a100ff708e4476e283c to your computer and use it in GitHub Desktop.

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
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment