Last active
December 23, 2015 00:59
-
-
Save fdstevex/6557526 to your computer and use it in GitHub Desktop.
Simple Core Data example showing how long it takes to create 10,000 records and then update those records.
This file contains 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
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
NSURL *db = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestDB.sqlite"]; | |
[[NSFileManager defaultManager] removeItemAtURL:db error:nil]; | |
NSManagedObjectContext *moc = [self managedObjectContext]; | |
NSLog(@"Creating Entities"); | |
for (int i=0; i<10000; i++) { | |
MyEntity *entity = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:moc]; | |
entity.name = [NSString stringWithFormat:@"Entity-%d", i]; | |
} | |
NSError *error = nil; | |
if (![moc save:&error]) { | |
NSLog(@"Error: %@", error); | |
} | |
// Flush the context, so that fetching all the entities will have to hit the database | |
[moc reset]; | |
NSLog(@"Created and saved 10,000 entities"); | |
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"]; | |
request.includesPropertyValues = NO; | |
NSArray *objects = [moc executeFetchRequest:request error:nil]; | |
for (MyEntity *entity in objects) { | |
entity.read = @YES; | |
} | |
if (![moc save:&error]) { | |
NSLog(@"Error: %@", error); | |
} | |
NSLog(@"Updated 10,000 entities"); | |
// Override point for customization after application launch. | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment