Skip to content

Instantly share code, notes, and snippets.

@CavalcanteLeo
Forked from calebhicks/CoreDataHelper.m
Created March 22, 2016 02:40
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 CavalcanteLeo/5c08e3a2c97144ce5977 to your computer and use it in GitHub Desktop.
Save CavalcanteLeo/5c08e3a2c97144ce5977 to your computer and use it in GitHub Desktop.
Core Data Helper
//
// CoreDataHelper.m
// Wired In
//
// Created by Caleb Hicks on 6/20/14.
//
// Readme: I use a version of this file in each Core Data project I create. It runs as a Shared Instance.
// Simply, it returns the default Managed Object Context for the app. Sample methods also provided to save, create sample
// data, and log all data within the app. Adjust the Imports and related methods for your model objects.
//
// Enjoy! Any questions? Comment below or @calebhicks on Twitter.
#import "CoreDataHelper.h"
#import "WIAppDelegate.h"
#import "WIRoundController.h"
#import "Tag.h"
#import "Task.h"
#import "Round.h"
@implementation CoreDataHelper
+ (CoreDataHelper *)sharedInstance {
static CoreDataHelper *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [CoreDataHelper new];
});
return sharedInstance;
}
- (NSManagedObjectContext *)managedObjectContext{
WIAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
return managedObjectContext;
}
- (void) save{
[self.managedObjectContext save:nil];
}
-(void)setUpSampleData: (NSInteger)withIdentifier{
Tag *tag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
tag.tagName = [NSString stringWithFormat: @"awesomeTag %ld", (long)withIdentifier];
Round *round = [NSEntityDescription insertNewObjectForEntityForName:@"Round" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
round.duration = @1200;
round.startTime = [NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]];
round.endTime = [NSDate dateWithTimeInterval:1200 sinceDate:[NSDate date]];
round.roundDescription = [NSString stringWithFormat: @"Round Description %ld", (long)withIdentifier];
Task *task = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
task.duration = @1200;
task.startTime = [NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]];
task.finishTime = [NSDate dateWithTimeInterval:1200 sinceDate:[NSDate date]];
task.taskName = [NSString stringWithFormat:@"task name %ld", (long)withIdentifier];
task.complete = @1;
round.tasks = [NSSet setWithArray: @[task]];
task.tags = [NSSet setWithArray: @[tag]];
}
- (void)logCurrentData{
NSFetchRequest* roundRequest = [NSFetchRequest fetchRequestWithEntityName:@"Round"];
NSArray *rounds = [[[CoreDataHelper sharedInstance]managedObjectContext] executeFetchRequest:roundRequest error:NULL];
for (Round *round in rounds) {
NSLog(@"Round Description: %@", round.roundDescription);
for (Task *task in round.tasks) {
NSLog(@"Task Named: %@", task.taskName);
for (Tag *tag in task.tags) {
NSLog(@"Tag Named: %@", tag.tagName);
}
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment