coredata访问数据关系故障 [英] coredata accessing data relationship fault
本文介绍了coredata访问数据关系故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有coredata项目这里是我的.h文件:
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;
我填充我的coredata如下:
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'>";
如何访问内容标签?
推荐答案
我想出了一个不同的方法:
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"]);
这篇关于coredata访问数据关系故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文