Skip to content

Commit

Permalink
Now using NSManagedObject category.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorey committed Feb 28, 2014
1 parent 71e49a5 commit 7f761ce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="13B42" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="WeightLog" representedClassName="WTWeightLog" syncable="YES">
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="3401" systemVersion="13C64" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="WTWeightLog" representedClassName="WTWeightLog" syncable="YES">
<attribute name="dateTaken" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="units" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="weight" optional="YES" attributeType="Float" defaultValueString="0.0" syncable="YES"/>
</entity>
<elements>
<element name="WeightLog" positionX="0" positionY="0" width="0" height="0"/>
<element name="WTWeightLog" positionX="0" positionY="0" width="0" height="0"/>
</elements>
</model>
25 changes: 8 additions & 17 deletions 109-mdmcoredata/WeightTracker/WeightTracker/ViewController.m
Expand Up @@ -33,7 +33,7 @@ - (void)viewDidLoad {
}

- (void)loadLastWeight {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WeightLog"];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[WTWeightLog MDMCoreDataAdditionsEntityName]];
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"dateTaken" ascending:NO] ];
fetchRequest.fetchLimit = 1;

Expand Down Expand Up @@ -74,28 +74,19 @@ - (IBAction)unwindFromRecordWeight:(UIStoryboardSegue *)recordWeightSegue {
}

- (void)saveWeight:(CGFloat)weight {
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"WeightLog"
inManagedObjectContext:self.persistenceController.managedObjectContext];
WTWeightLog *log = [[WTWeightLog alloc] initWithEntity:entityDesc
insertIntoManagedObjectContext:self.persistenceController.managedObjectContext];

WTWeightLog *log = [WTWeightLog MDMCoreDataAdditionsInsertNewObjectIntoContext:self.persistenceController.managedObjectContext];
log.dateTaken = [NSDate date];
log.units = @"lbs";
log.weight = @(weight);

[self updateForWeight:log];

NSError *error;
if([self.persistenceController.managedObjectContext save:&error]) {
NSLog(@"Saved");

[self.persistenceController saveContextAndWait:YES completion:^(NSError *error) {
if (error) {
NSLog(@"ERROR:%@", [error localizedDescription]);
}
}];
} else {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
[self.persistenceController saveContextAndWait:NO completion:^(NSError *error) {
if (error) {
NSLog(@"ERROR:%@", [error localizedDescription]);
}
}];
}

- (void)updateForWeight:(WTWeightLog *)log {
Expand Down
Expand Up @@ -50,7 +50,7 @@ - (void)viewDidLoad {
}

- (NSFetchRequest *)recentEntriesFetchRequest {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WeightLog"];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[WTWeightLog MDMCoreDataAdditionsEntityName]];
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"dateTaken" ascending:NO] ];
return fetchRequest;
}
Expand Down

0 comments on commit 7f761ce

Please sign in to comment.