-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyAlbum.m
66 lines (51 loc) · 1.69 KB
/
MyAlbum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// MyAlbum.m
// Gallery
//
// Created by David Kapp on 2/27/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MyAlbum.h"
@implementation MyAlbum
// remember, use @dynamic for Core Data properties, not @synthesize
@dynamic title;
@dynamic photos;
+ (id) albumInDefaultContext {
NSManagedObjectContext *context = [[NSApp delegate] managedObjectContext];
MyAlbum *newItem;
newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Album"
inManagedObjectContext:context];
return newItem;
}
+ (id) defaultAlbum {
NSManagedObjectContext *context = [[NSApp delegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album"
inManagedObjectContext:context];
// create a fetch request to find the 'Default' album
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
fetch.entity = entity;
fetch.predicate = [NSPredicate predicateWithFormat:@"title == 'Default'"];
// run fetch and make sure it succeeded
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fetch error:&error];
[fetch release];
if (error) {
NSLog(@"Error trying to do fetch: %@", error);
return nil;
}
// create the album if it doesn't exist
MyAlbum *album = nil;
if (results.count > 0) {
album = [results objectAtIndex:0];
}
else {
album = [self albumInDefaultContext];
album.title = @"Default";
}
return album;
}
// used by list view
- (NSImage*) image {
return [NSImage imageNamed:NSImageNameFolder];
}
@end