Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added CoreDataExamples/.DS_Store
Binary file not shown.
580 changes: 580 additions & 0 deletions CoreDataExamples/CoreDataExamples.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// AppDelegate.h
// SavingForever
// CoreDataExamples
//
// Created by Michael Kavouras on 10/4/15.
// Copyright © 2015 Michael Kavouras. All rights reserved.
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import <UIKit/UIKit.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// AppDelegate.m
// SavingForever
// CoreDataExamples
//
// Created by Michael Kavouras on 10/4/15.
// Copyright © 2015 Michael Kavouras. All rights reserved.
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import "AppDelegate.h"
Expand Down Expand Up @@ -51,7 +51,7 @@ - (void)applicationWillTerminate:(UIApplication *)application {
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.mikekavouras.SavingForever" in the application's documents directory.
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.jasonwang.CoreDataExamples" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Expand All @@ -60,7 +60,7 @@ - (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SavingForever" withExtension:@"momd"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataExamples" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
Expand All @@ -74,7 +74,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"SavingForever.sqlite"];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataExamples.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
Expand Down
214 changes: 214 additions & 0 deletions CoreDataExamples/CoreDataExamples/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>SavingForever.xcdatamodel</string>
<string>CoreDataExamples.xcdatamodel</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="8195" systemVersion="15A284" minimumToolsVersion="Automatic">
<entity name="List" representedClassName="List" syncable="YES">
<attribute name="color" optional="YES" attributeType="Transformable" syncable="YES"/>
<attribute name="createdAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="task" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Task" inverseName="list" inverseEntity="Task" syncable="YES"/>
</entity>
<entity name="Task" representedClassName="Task" syncable="YES">
<attribute name="completedAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="createdAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="dueAt" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="priority" optional="YES" attributeType="Integer 64" defaultValueString="0" syncable="YES"/>
<attribute name="taskDescription" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="updatedAt" optional="YES" attributeType="Date" syncable="YES"/>
<relationship name="list" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="List" inverseName="task" inverseEntity="List" syncable="YES"/>
</entity>
<elements>
<element name="Task" positionX="-63" positionY="-18" width="128" height="150"/>
<element name="List" positionX="-308" positionY="-9" width="128" height="103"/>
</elements>
</model>
25 changes: 25 additions & 0 deletions CoreDataExamples/CoreDataExamples/List+CoreDataProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// List+CoreDataProperties.h
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//

#import "List.h"

NS_ASSUME_NONNULL_BEGIN

@interface List (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) NSDate *createdAt;
@property (nullable, nonatomic, retain) id color;
@property (nullable, nonatomic, retain) NSArray *task;

@end

NS_ASSUME_NONNULL_END
21 changes: 21 additions & 0 deletions CoreDataExamples/CoreDataExamples/List+CoreDataProperties.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// List+CoreDataProperties.m
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//

#import "List+CoreDataProperties.h"

@implementation List (CoreDataProperties)

@dynamic title;
@dynamic createdAt;
@dynamic color;
@dynamic task;

@end
24 changes: 24 additions & 0 deletions CoreDataExamples/CoreDataExamples/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// List.h
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Task;

NS_ASSUME_NONNULL_BEGIN

@interface List : NSManagedObject

// Insert code here to declare functionality of your managed object subclass

@end

NS_ASSUME_NONNULL_END

#import "List+CoreDataProperties.h"
16 changes: 16 additions & 0 deletions CoreDataExamples/CoreDataExamples/List.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// List.m
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import "List.h"
#import "Task.h"

@implementation List

// Insert code here to add functionality to your managed object subclass

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ListsCreationTableViewController.h
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ListsCreationTableViewController : UITableViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// ListsCreationTableViewController.m
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import <CoreData/CoreData.h>
#import "ListsCreationTableViewController.h"
#import "List.h"
#import "AppDelegate.h"


@interface ListsCreationTableViewController ()

@property (strong, nonatomic) IBOutlet UITextField *titleTextField;

@property (nonatomic) List *list;

@end

@implementation ListsCreationTableViewController

-(void)viewDidLoad {
[super viewDidLoad];

[self setupNavigationBar];

AppDelegate *delegate = [UIApplication sharedApplication].delegate;

self.list = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:delegate.managedObjectContext];
NSLog(@"%@", self.list);
}

-(void)setupNavigationBar {

//set the title
self.navigationItem.title = @"Create New List";

//set the left bar to cancel
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];

//set the right button to save
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];

}

-(void)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)save {
self.list.title = self.titleTextField.text;
self.list.createdAt = [[NSDate alloc] init];
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[delegate.managedObjectContext save:nil];


}

- (IBAction)colorButtonTapped:(UIButton *)sender {

self.list.color = sender.backgroundColor;

}


@end
13 changes: 13 additions & 0 deletions CoreDataExamples/CoreDataExamples/ListsTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ListsTableViewController.h
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ListsTableViewController : UITableViewController

@end
84 changes: 84 additions & 0 deletions CoreDataExamples/CoreDataExamples/ListsTableViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// ListsTableViewController.m
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//

#import "ListsTableViewController.h"
#import "AppDelegate.h"
#import "List.h"
#import <CoreData/CoreData.h>

@interface ListsTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic) NSFetchedResultsController *fetchedResultsController;

@end

@implementation ListsTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

AppDelegate *delegate = [UIApplication sharedApplication].delegate;

// 1) create an instance of NSFetchRequest with an entity name
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"List"];

// 2) create a sort descriptor
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO];

// 3) set the sortDescriptors on the fetchRequest
fetchRequest.sortDescriptors = @[sort];

// 4) create a fetchedResultsController with a fetchRequest and a managedObject
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:delegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

self.fetchedResultsController.delegate = self;

[self.fetchedResultsController performFetch:nil];

[self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.fetchedResultsController.fetchedObjects.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCellIdentifier" forIndexPath:indexPath];

List *list = self.fetchedResultsController.fetchedObjects[indexPath.row];

cell.backgroundColor = (UIColor *)list.color;

cell.textLabel.text = list.title;

cell.detailTextLabel.text = [list.createdAt description];

return cell;
}

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
[self.tableView reloadData];
}



@end
28 changes: 28 additions & 0 deletions CoreDataExamples/CoreDataExamples/Task+CoreDataProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Task+CoreDataProperties.h
// CoreDataExamples
//
// Created by Jason Wang on 10/4/15.
// Copyright © 2015 Jason Wang. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//

#import "Task.h"

NS_ASSUME_NONNULL_BEGIN

@interface Task (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *taskDescription;
@property (nullable, nonatomic, retain) NSDate *createdAt;
@property (nullable, nonatomic, retain) NSDate *dueAt;
@property (nullable, nonatomic, retain) NSDate *updatedAt;
@property (nullable, nonatomic, retain) NSNumber *priority;
@property (nullable, nonatomic, retain) NSDate *completedAt;
@property (nullable, nonatomic, retain) NSManagedObject *list;

@end

NS_ASSUME_NONNULL_END
Loading