Last active
August 29, 2015 14:18
-
-
Save 3ign0n/c5fd12ad6fe63a174d02 to your computer and use it in GitHub Desktop.
Realm concurrency write test with huge data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <XCTest/XCTest.h> | |
#import <Realm/Realm.h> | |
@interface Person: RLMObject | |
@property (nonatomic, copy) NSString* name; | |
@property (nonatomic, copy) NSDate* birthdate; | |
@end | |
@implementation Person | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_name = @""; | |
_birthdate = [NSDate date]; | |
} | |
return self; | |
} | |
@end | |
@interface RealmConcurrencyTest : XCTestCase | |
@end | |
@implementation RealmConcurrencyTest | |
- (void)setUp { | |
[super setUp]; | |
// Put setup code here. This method is called before the invocation of each test method in the class. | |
} | |
- (void)tearDown { | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
[super tearDown]; | |
} | |
- (void)testExample { | |
[self loadData]; | |
} | |
- (void)loadData { | |
[self asyncDataWrite:0 range: NSMakeRange(0, 10000)]; | |
[self asyncDataWrite:0 range: NSMakeRange(10000, 20000)]; | |
[self asyncDataWrite:0 range: NSMakeRange(20000, 30000)]; | |
[self asyncDataWrite:0 range: NSMakeRange(30000, 40000)]; | |
[self asyncDataWrite:0 range: NSMakeRange(40000, 50000)]; | |
} | |
- (void)asyncDataWrite:(NSInteger)queueId | |
range:(NSRange)range { | |
NSInteger counter = range.location; | |
while (counter <range.length) { | |
const NSInteger writeBlockCount = 200; | |
NSMutableArray* authors = [@[] mutableCopy]; | |
for (NSInteger i=0; i<writeBlockCount; i++) { | |
Person *author = [[Person alloc] init]; | |
author.name = [NSString stringWithFormat:@"David Foster Wallace %lu", counter+i]; | |
[authors addObject:author]; | |
} | |
const char* queneName = [[NSString stringWithFormat:@"queue%lu", queueId] cStringUsingEncoding:NSUTF8StringEncoding]; | |
dispatch_queue_t queue = dispatch_queue_create(queneName, DISPATCH_QUEUE_CONCURRENT); | |
// It works good with synchronize dispatch | |
//dispatch_sync(queue, ^{ | |
dispatch_async(queue, ^{ | |
RLMRealm *realm = [RLMRealm defaultRealm]; | |
// You only need to do this once (per thread) | |
// Add to the Realm inside a transaction | |
[realm beginWriteTransaction]; | |
// You may not observe the behavior when enabling NSLog. | |
// Because it causes execution slow down. | |
//NSLog(@"#######writing array.count=%lu", authors.count); | |
[realm addObjects:authors]; | |
//RLMResults* results = [[authors[0] class] performSelector:@selector(allObjects)]; | |
//NSLog(@"#######read array.count=%lu", results.count); | |
[realm commitWriteTransaction]; | |
}); | |
counter += writeBlockCount; | |
} | |
} | |
- (void)testReadAll { | |
RLMRealm *realm = [RLMRealm defaultRealm]; | |
RLMResults *results = [Person allObjectsInRealm:realm]; | |
NSLog(@"#######count=%lu", (unsigned long)results.count); | |
/* | |
NSInteger i=0; | |
for (id obj in results) { | |
Person *person = (Person*)obj; | |
NSLog(@"results[%lu].name=%@", i, person.name); | |
i++; | |
} | |
*/ | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment