coredata accessing data relationship fault

2019-09-17 21:49发布

问题:

I have coredata project here is my .h file:

@interface Content : NSManagedObject

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * body;
@property (nonatomic, retain) Categories *category;
@property (nonatomic, retain) NSSet *tags;
@end

@interface Content (CoreDataGeneratedAccessors)

- (void)addTagsObject:(Tags *)value;
- (void)removeTagsObject:(Tags *)value;
- (void)addTags:(NSSet *)values;
- (void)removeTags:(NSSet *)values;

And I'm populating the my coredata as follows:

    NSError *error = nil;

    // Categories:

    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *contentDescription = [ NSEntityDescription entityForName:@"Categories" inManagedObjectContext:moc];

    NSFetchRequest *categoRequest = [NSFetchRequest new];
    categoRequest.entity = contentDescription;
    NSPredicate *categoPredicate = [NSPredicate predicateWithFormat:@"category like %@", _dropMenuOulet.stringValue];
    categoRequest.predicate = categoPredicate;

    NSArray *results = [moc executeFetchRequest:categoRequest error:&error];

    Categories *catego = (Categories*) [results objectAtIndex:0];

//    Tags:

    NSEntityDescription *tagsDescription = [ NSEntityDescription entityForName:@"Tags" inManagedObjectContext:moc];
    NSFetchRequest *tagsRequest = [NSFetchRequest new];
    tagsRequest.entity = tagsDescription;
    NSArray *tagsResults = [moc executeFetchRequest:tagsRequest error:&error];


    Content *content1 = [NSEntityDescription insertNewObjectForEntityForName:@"Content" inManagedObjectContext:moc];

    content1.category = catego;
    content1.title = _titleOutlet.stringValue;
    content1.body = _bodyOutlet.stringValue;

    NSMutableSet  *tagSet = [NSMutableSet set];


    for (NSManagedObject *obj in tagsResults)
    {
        [tagSet addObject:obj];
    }

    content1.tags = tagSet;

My problem right now is when I try to access to the tags in the content I get the following:

tags = "<relationship fault: 0x60000002d820 'tags'>";

How can access to the tags of the content?

回答1:

I figure out doing it in a different way:

NSError *error = nil;
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *contentDescription = [ NSEntityDescription entityForName:@"Content" inManagedObjectContext:moc];
    NSFetchRequest *contentRequest = [NSFetchRequest new];
    contentRequest.entity = contentDescription;
    NSArray *results = [moc executeFetchRequest:contentRequest error:&error];
    NSLog(@"results content %@",results);

    Content *myCont = [results objectAtIndex:0];

    NSLog(@"list of tags %@", [[myCont tags] valueForKey:@"tag"]);