Proper handling of tags in data model

This commit is contained in:
Matthias Neeracher 2011-08-13 16:20:06 +02:00
parent 96a01add73
commit 0db88816a4
5 changed files with 51 additions and 7 deletions

View File

@ -22,6 +22,14 @@
- (NSString *)shortLocation;
+ (NSSet *)keyPathsForValuesAffectingShortLocation;
/*
* Tags are never manipulated through the tag objects, but always through
* tag description objects.
*/
- (NSArray *)tagDescriptions;
- (void)setTagDescriptions:(NSArray *)tagDescriptions;
+ (NSSet *)keyPathsForValuesAffectingTagDescriptions;
@end
@interface MAAnno (CoreDataGeneratedAccessors)

View File

@ -8,7 +8,9 @@
#import "MAAnno.h"
#import "MATag.h"
#import "MATagDescription.h"
#import <vector>
@implementation MAAnno
@dynamic location;
@ -49,4 +51,35 @@
return [NSSet setWithObject:@"location"];
}
- (NSArray *)tagDescriptions
{
NSSet * tags = self.tags;
std::vector<NSManagedObject *> descs([tags count]);
for (MATag * tag in tags)
descs[[tag seqNo]] = [tag tag];
return [NSArray arrayWithObjects:&descs[0] count:descs.size()];
}
- (void)setTagDescriptions:(NSArray *)tagDescriptions
{
NSManagedObjectContext *moc = [self managedObjectContext];
int seqNo = 0;
std::vector<MATag *> tags;
for (MATagDescription * tagDesc in tagDescriptions) {
MATag * tag = [NSEntityDescription insertNewObjectForEntityForName:@"MATag" inManagedObjectContext:moc];
tag.seqNo = seqNo++;
tag.tag = tagDesc;
tag.annotation = self;
tags.push_back(tag);
}
[self setTags:[NSSet setWithObjects:&tags[0] count:tags.size()]];
}
+ (NSSet *)keyPathsForValuesAffectingTagDescriptions
{
return [NSSet setWithObject:@"tags"];
}
@end

View File

@ -4,7 +4,7 @@
<attribute name="location" attributeType="String"/>
<attribute name="notes" attributeType="String"/>
<relationship name="media" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MAMedia" inverseName="annotations" inverseEntity="MAMedia"/>
<relationship name="tags" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MATag" inverseName="annotation" inverseEntity="MATag"/>
<relationship name="tags" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="MATag" inverseName="annotation" inverseEntity="MATag"/>
</entity>
<entity name="MAFolder" representedClassName="MAFolder">
<attribute name="name" attributeType="String"/>
@ -17,23 +17,24 @@
<attribute name="media" attributeType="String"/>
<attribute name="name" attributeType="String"/>
<attribute name="notes" attributeType="String"/>
<relationship name="annotations" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MAAnno" inverseName="media" inverseEntity="MAAnno"/>
<relationship name="annotations" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="MAAnno" inverseName="media" inverseEntity="MAAnno"/>
<relationship name="folder" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MAFolder" inverseName="contents" inverseEntity="MAFolder"/>
</entity>
<entity name="MATag" representedClassName="MATag">
<attribute name="seqNo" attributeType="Integer 16" defaultValueString="0"/>
<relationship name="annotation" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MAAnno" inverseName="tags" inverseEntity="MAAnno"/>
<relationship name="tag" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MATagDescription" inverseName="uses" inverseEntity="MATagDescription"/>
</entity>
<entity name="MATagDescription" representedClassName="MATagDescription">
<attribute name="name" attributeType="String"/>
<attribute name="notes" optional="YES" attributeType="String"/>
<relationship name="uses" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MATag" inverseName="tag" inverseEntity="MATag"/>
<attribute name="notes" attributeType="String"/>
<relationship name="uses" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="MATag" inverseName="tag" inverseEntity="MATag"/>
</entity>
<elements>
<element name="MAMedia" positionX="-1116" positionY="-882" width="128" height="135"/>
<element name="MATagDescription" positionX="-866" positionY="-576" width="128" height="90"/>
<element name="MAAnno" positionX="-1296" positionY="-585" width="128" height="105"/>
<element name="MAFolder" positionX="-864" positionY="-873" width="180" height="105"/>
<element name="MATag" positionX="-1044" positionY="-684" width="128" height="75"/>
<element name="MAMedia" positionX="-1116" positionY="-882" width="128" height="135"/>
<element name="MATag" positionX="-1044" positionY="-678" width="126" height="75"/>
<element name="MATagDescription" positionX="-866" positionY="-576" width="128" height="90"/>
</elements>
</model>

View File

@ -13,6 +13,7 @@
@interface MATag : NSManagedObject {
@private
}
@property (nonatomic) int seqNo;
@property (nonatomic, retain) NSManagedObject *tag;
@property (nonatomic, retain) NSManagedObject *annotation;

View File

@ -10,6 +10,7 @@
@implementation MATag
@dynamic seqNo;
@dynamic tag;
@dynamic annotation;