Skip to content

Instantly share code, notes, and snippets.

@Daivest
Last active August 12, 2019 08:56
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 Daivest/c6b5dfe9453124ffff8549131b52748e to your computer and use it in GitHub Desktop.
Save Daivest/c6b5dfe9453124ffff8549131b52748e to your computer and use it in GitHub Desktop.
Testing realm performance
#import "RLMObject.h"
@class Message;
@interface Conversation : RLMObject
@property NSString *peerId;
@property NSDate *mostRecentDate;
@property Message *mostRecentMessage;
@end
RLM_ARRAY_TYPE(Conversation)
@implementation Conversation
+ (NSString *)primaryKey {
return @"peerId";
}
@end
#import "RLMObject.h"
@interface Message : RLMObject
@property NSString *Id;
@property NSString *peerId;
@property NSDate *recordingDate;
@property NSString *text;
@end
@implementation Message
+ (NSString *)primaryKey {
return @"Id";
}
+ (NSArray<NSString *> *)indexedProperties {
return @[@"peerId", @"recordingDate"];
}
@end
#import "ListViewController.h"
#import <Realm/Realm.h>
#import "Message.h"
#import "Conversation.h"
@interface ListViewController ()
@property NSMutableArray<RLMResults *> *mResults;
@property NSMutableArray<RLMNotificationToken *> *mNotificationToken;
@property RLMRealm *realm;
@property BOOL disableSort;
@property BOOL disableInjectMessages;
@end
@implementation ListViewController
- (void)viewDidLoad {
[super viewDidLoad];
_disableSort = NO;
_disableInjectMessages = NO;
_mResults = [NSMutableArray new];
_mNotificationToken = [NSMutableArray new];
_realm = [RLMRealm defaultRealm];
[self _generateConversations];
}
#pragma mark - Actions
- (IBAction)addMessages:(id)sender {
NSMutableArray *msgs = [NSMutableArray new];
for (NSInteger i = 0; i < 200; i ++) {
Message *msg = [[Message alloc] init];
msg.Id = [NSUUID UUID].UUIDString;
msg.text = [NSUUID UUID].UUIDString;
msg.peerId = @"first";
msg.recordingDate = [NSDate date];
[msgs addObject:msg];
}
[self _saveMessages:msgs];
if (_disableInjectMessages == NO) {
[self _injectMessages:msgs];
}
}
- (IBAction)configureMessageResults:(id)sender {
for (NSInteger i = 0; i < 5; i ++) {
RLMResults *res = [self _getMessagesResultsForConversationId:@"first"];
[_mResults addObject:res];
RLMNotificationToken *tok = [res addNotificationBlock:^(RLMResults * _Nullable results, RLMCollectionChange * _Nullable change, NSError * _Nullable error) {
NSLog(@"== receive notification messages ==");
}];
[_mNotificationToken addObject:tok];
}
}
#pragma mark - Inject
- (void)_injectMessages:(NSArray<Message *> *)messages {
[_realm beginWriteTransaction];
Conversation *conv = [Conversation objectInRealm:_realm forPrimaryKey:messages.lastObject.peerId];
conv.mostRecentMessage = messages.lastObject;
conv.mostRecentDate = messages.lastObject.recordingDate;
[_realm addOrUpdateObjects:@[conv]];
[_realm commitWriteTransaction];
}
#pragma mark - Generate
- (void)_generateConversations {
NSMutableArray *conversations = [NSMutableArray new];
for (NSInteger i = 0; i < 4; i ++) {
Conversation *conv = [[Conversation alloc] init];
NSArray *indexes = @[@"first", @"second", @"third", @"forth"];
conv.peerId = indexes[i];
[conversations addObject:conv];
}
[self _saveConversations:conversations];
}
#pragma mark - Storage
- (void)_saveMessages:(NSArray<Message *> *)messages {
RLMRealm *realm = _realm;
[realm beginWriteTransaction];
[realm addOrUpdateObjects:messages];
[realm commitWriteTransaction];
}
- (void)_saveConversations:(NSArray<Conversation *> *)conversations {
RLMRealm *realm = _realm;
[realm beginWriteTransaction];
[realm addOrUpdateObjects:conversations];
[realm commitWriteTransaction];
}
- (RLMResults *)_getMessagesResultsForConversationId:(NSString *)conversationId {
RLMRealm *realm = _realm;
NSString *filter = [NSString stringWithFormat:@"peerId == '%@'", conversationId];
RLMResults *results = [Message objectsInRealm:realm where:filter];
if (_disableSort == NO) {
RLMSortDescriptor *sortDescr = [RLMSortDescriptor sortDescriptorWithKeyPath:@"recordingDate" ascending:NO];
results = [results sortedResultsUsingDescriptors:@[sortDescr]];
}
return results;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment