Count operation very slow since Realm 3.17.1

(Promise Xu) #1

My app experiences significant performance issue after updating Realm to 3.17.1+. In my tableViewController, in “viewDidLoad:”, I generate a realm result:

NSDate *start = [NSDate date]; // Test speed
self.localPostChatRecords = [[KCLocalChatRecord objectsInRealm:realm withPredicate:[NSPredicate predicateWithFormat:@"chatType == %i", KCChatTypePost]] sortedResultsUsingDescriptors:@[[RLMSortDescriptor sortDescriptorWithKeyPath:@"sticky" ascending:NO], [RLMSortDescriptor sortDescriptorWithKeyPath:@"lastUpdatedAt" ascending:NO]]];
NSLog(@"Execution Time 1: %f *\n", [[NSDate date] timeIntervalSinceDate:start]); // Test speed
NSLog(@"Number of entries: %@", @(self.localPostChatRecords.count));
NSLog(@"Execution Time 2: %f *\n", [[NSDate date] timeIntervalSinceDate:start]); // Test speed

Outputs for “Execution Time 1” and “Execution Time 2” are around 0.017s and 0.028s, so here seems to be fine. But a significant delay is found in “tableView:numberOfRowsInSection:”.

Here I calculate the number of rows as follows:

NSDate *start = [NSDate date]; // Test speed
NSUInteger count = self.localPostChatRecords.count;
NSLog(@"Execution Time: %f\n", [[NSDate date] timeIntervalSinceDate:start]); // Test speed
return count;

The output for “Execution Time” is around 1.6s (in realm 3.17.0, it is around 0.005s). Hence there is a major non-responsive time when users try to open this tableViewController. The issue is present in all versions since 3.17.1+.

Why counting the realm result is so much slower in “tableView:numberOfRowsInSection:”? Is this a bug and is there any workaround solution?

Thank you!

(Brian Munkholm) #2

As this appears to be a regression (bug), I would encourage you to create an issue in Github. If you can provide a self contained minimal project that reproduce this, then it will be much appreciated!