forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 26
/
CHMTag.m
75 lines (66 loc) · 1.9 KB
/
CHMTag.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
67
68
69
70
71
72
73
74
75
//
// CHMTag.m
// ichm
//
// Created by Robin Lu on 8/10/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMTag.h"
#import "CHMBookmark.h"
@implementation CHMTag
@dynamic tag;
@dynamic bookmarks;
+ (CHMTag*)getTagByString:(NSString*)tagstr OnCreate:(BOOL)shouldCreate withContext:(NSManagedObjectContext*)context
{
if (!tagstr)
return nil;
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *tagEntity = [NSEntityDescription
entityForName:@"Tag" inManagedObjectContext:context];
[request setEntity:tagEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"tag == %@", tagstr];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if (array == nil)
{
NSLog(@"Cannot fetch tag info: %@", error);
return nil;
}
if ([array count] != 0)
return [array objectAtIndex:0];;
if (shouldCreate)
{
CHMTag *tag = [NSEntityDescription
insertNewObjectForEntityForName:@"Tag"
inManagedObjectContext:context];
tag.tag = tagstr;
if ( ![context save:&error] )
{
NSLog(@"Cannot create tag: %@", error);
return nil;
}
return tag;
}
return nil;
}
+ (NSArray*)allTagswithContext:(NSManagedObjectContext*)context
{
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *tagEntity = [NSEntityDescription
entityForName:@"Tag" inManagedObjectContext:context];
[request setEntity:tagEntity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"tag" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if (array == nil)
{
NSLog(@"Cannot fetch tag info: %@", error);
}
return array;
}
@end