Skip to content

Commit

Permalink
added fetchDataByTag
Browse files Browse the repository at this point in the history
  • Loading branch information
xcatsan committed May 21, 2010
1 parent 380abf1 commit 49424f1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
2F7446990DB6B7EA00F9684A /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2F7446970DB6B7EA00F9684A /* MainMenu.xib */; };
4C93731811A4EF51009D21D7 /* BlogEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C93731511A4EF51009D21D7 /* BlogEntry.m */; };
4C93731911A4EF51009D21D7 /* Tag.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C93731711A4EF51009D21D7 /* Tag.m */; };
4C9373DE11A60933009D21D7 /* Tag.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C9373DD11A60933009D21D7 /* Tag.m */; };
770B37ED0679A11B001EADE2 /* CoreDataRelationship_DataModel.xcdatamodel in Sources */ = {isa = PBXBuildFile; fileRef = 770B37EC0679A11B001EADE2 /* CoreDataRelationship_DataModel.xcdatamodel */; };
77C8280E06725ACE000B614F /* CoreDataRelationship_AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C8280C06725ACE000B614F /* CoreDataRelationship_AppDelegate.m */; };
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
Expand All @@ -27,8 +27,8 @@
32CA4F630368D1EE00C91783 /* CoreDataRelationship_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoreDataRelationship_Prefix.pch; sourceTree = "<group>"; };
4C93731411A4EF51009D21D7 /* BlogEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlogEntry.h; sourceTree = "<group>"; };
4C93731511A4EF51009D21D7 /* BlogEntry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BlogEntry.m; sourceTree = "<group>"; };
4C93731611A4EF51009D21D7 /* Tag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tag.h; sourceTree = "<group>"; };
4C93731711A4EF51009D21D7 /* Tag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Tag.m; sourceTree = "<group>"; };
4C9373DC11A60933009D21D7 /* Tag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tag.h; sourceTree = "<group>"; };
4C9373DD11A60933009D21D7 /* Tag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Tag.m; sourceTree = "<group>"; };
770B37EC0679A11B001EADE2 /* CoreDataRelationship_DataModel.xcdatamodel */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = wrapper.xcdatamodel; path = CoreDataRelationship_DataModel.xcdatamodel; sourceTree = "<group>"; };
77C82804067257F0000B614F /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
77C8280B06725ACE000B614F /* CoreDataRelationship_AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoreDataRelationship_AppDelegate.h; sourceTree = "<group>"; };
Expand All @@ -52,10 +52,10 @@
080E96DDFE201D6D7F000001 /* Classes */ = {
isa = PBXGroup;
children = (
4C9373DC11A60933009D21D7 /* Tag.h */,
4C9373DD11A60933009D21D7 /* Tag.m */,
4C93731411A4EF51009D21D7 /* BlogEntry.h */,
4C93731511A4EF51009D21D7 /* BlogEntry.m */,
4C93731611A4EF51009D21D7 /* Tag.h */,
4C93731711A4EF51009D21D7 /* Tag.m */,
77C8280B06725ACE000B614F /* CoreDataRelationship_AppDelegate.h */,
77C8280C06725ACE000B614F /* CoreDataRelationship_AppDelegate.m */,
);
Expand Down Expand Up @@ -196,7 +196,7 @@
77C8280E06725ACE000B614F /* CoreDataRelationship_AppDelegate.m in Sources */,
770B37ED0679A11B001EADE2 /* CoreDataRelationship_DataModel.xcdatamodel in Sources */,
4C93731811A4EF51009D21D7 /* BlogEntry.m in Sources */,
4C93731911A4EF51009D21D7 /* Tag.m in Sources */,
4C9373DE11A60933009D21D7 /* Tag.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
42 changes: 38 additions & 4 deletions CoreDataRelationship/CoreDataRelationship_AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ - (void)addTestData
}

NSManagedObjectContext* moc = [self managedObjectContext];
NSError* error = nil;

Tag* tag1 = [NSEntityDescription insertNewObjectForEntityForName:@"Tag"
inManagedObjectContext:moc];
Expand All @@ -223,12 +224,21 @@ - (void)addTestData
Tag* tag3 = [NSEntityDescription insertNewObjectForEntityForName:@"Tag"
inManagedObjectContext:moc];
tag3.name = @"iPad";

[moc save:&error];
if (error) {
NSLog(@"INSERT ERROR: %@", error);
} else {
NSLog(@"INSERTED: Tag");
}


BlogEntry* blog1 = [NSEntityDescription insertNewObjectForEntityForName:@"BlogEntry"
inManagedObjectContext:moc];
blog1.title = @"CoreData のリレーションしっぷについて";
blog1.content = @"かくかくしかじか";
blog1.created = [NSDate date];
// [blog1 addTags:[NSSet setWithObjects:tag1, tag2, tag3, nil]];
blog1.tags = [NSSet setWithObjects:tag1, tag2, tag3, nil];

BlogEntry* blog2 = [NSEntityDescription insertNewObjectForEntityForName:@"BlogEntry"
Expand All @@ -237,14 +247,13 @@ - (void)addTestData
blog2.content = @"かくかくしかじか";
blog2.created = [NSDate date];
blog2.tags = [NSSet setWithObjects:tag3, nil];
// [blog2 addTags:[NSSet setWithObjects:tag3, nil]];

NSError* error = nil;
[moc save:&error];

if (error) {
NSLog(@"INSERT ERROR: %@", error);
} else {
NSLog(@"INSERTED");
NSLog(@"INSERTED: BlogEntry");
}
}

Expand All @@ -262,6 +271,7 @@ - (void)fetchData

NSLog(@"----- listup tag ---------------------------------------------------");
for (BlogEntry* entry in list) {
NSLog(@"title=%@", entry.title);
for (Tag* tag in entry.tags) {
NSLog(@"*tag*: %@", tag.name);
}
Expand All @@ -271,9 +281,33 @@ - (void)fetchData

}

- (void)fetchDataByTag
{
NSManagedObjectContext* moc = [self managedObjectContext];

NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Tag"
inManagedObjectContext:managedObjectContext]];

NSError* error = nil;
NSLog(@"----- executeFetchRequest ------------------------------------------");
NSArray* tags = [moc executeFetchRequest:request error:&error];

NSLog(@"----- listup entry ---------------------------------------------------");
for (Tag* tag in tags) {
NSLog(@"tag name=%@", tag.name);
for (BlogEntry* entry in tag.entries) {
NSLog(@"*entry*: %@", entry.title);
}
}

[request release];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self addTestData];
[self fetchData];
// [self fetchData];
[self fetchDataByTag];
}


Expand Down
Binary file not shown.
Binary file not shown.
11 changes: 10 additions & 1 deletion CoreDataRelationship/Tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
// Tag.h
// CoreDataRelationship
//
// Created by Hiroshi Hashiguchi on 10/05/20.
// Created by Hiroshi Hashiguchi on 10/05/21.
// Copyright 2010 . All rights reserved.
//

#import <CoreData/CoreData.h>

@class BlogEntry;

@interface Tag : NSManagedObject
{
}

@property (retain) NSString * name;
@property (retain) NSSet* entries;

@end

@interface Tag (CoreDataGeneratedAccessors)
- (void)addEntriesObject:(BlogEntry *)value;
- (void)removeEntriesObject:(BlogEntry *)value;
- (void)addEntries:(NSSet *)value;
- (void)removeEntries:(NSSet *)value;

@end

4 changes: 3 additions & 1 deletion CoreDataRelationship/Tag.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// Tag.m
// CoreDataRelationship
//
// Created by Hiroshi Hashiguchi on 10/05/20.
// Created by Hiroshi Hashiguchi on 10/05/21.
// Copyright 2010 . All rights reserved.
//

#import "Tag.h"

#import "BlogEntry.h"

@implementation Tag

@dynamic name;
@dynamic entries;

@end

0 comments on commit 49424f1

Please sign in to comment.