获取coredata关系 [英] fetch coredata relationship
问题描述
我想知道如何从我的核心数据关系船提取项目。我想它应该是一个字典或数组或返回的东西,以便你可以有你的一个很多东西。
I would like to know how to fetch the items from my coredata relation ship. I guess it should be a dictionary or arrays or something that gets returned so that you can have your one to many thing.
我很失落在这我知道如何写/读单个对象,但这种关系的东西是混乱的。
I am quite lost at this I know how to write/read single objects but this relationship stuff is abit confusing.
我认为我已经成功地写了一个关系coredata,但现在我想能够读它看看我是否有正确的..我已经开始写这个方法,但不知道实际做什么来获取所有的信息。
I think I have sucsessfully written a relationship to coredata however now I would like to be able to read it to see if I have it right.. I have started writing the method for this but have no idea what to actually do to get all of the information out.
这是代码我有这么多..为读和写
this is the code i have so far.. for both read and write
- (void)writeFin:(NSArray *)recivedProjectData ItemsData:(NSArray *)itemsData {
// WRITE TO CORE DATA
NSManagedObjectContext *context = [self managedObjectContext];
for (NSDictionary *dict in recivedProjectData) {
project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:context];
project.proNumber = [dict valueForKey:@"ProNumber"];
project.projectDescription = [dict valueForKey:@"Description"];
// project.items = [dict valueForKey:@""]; // this is the relationship for project
}
for (NSDictionary *dict in itemsData) {
items = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:context];
items.Number = [dict valueForKey:@"Number"];
items.Description = [dict valueForKey:@"Description"];
items.comment = [dict valueForKey:@"Comment"];
items.project = project; // this is the relationship for items
[project addItemsObject:items];
}
NSError *error = nil;
if (![__managedObjectContext save:&error]) {
NSLog(@"There was an error! %@", error);
}
else {
NSLog(@"created");
}
}
- (NSMutableArray *)readFin {
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (ProjectList *projectList in fetchedObjects) {
NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];
[tempProjectDictionaryArray setObject:project.proNumber forKey:@"ProNumber"];
[tempProjectDictionaryArray setObject:project.description forKey:@"Description"];
[tempProjectDictionaryArray setObject:project.projectID forKey:@"ProjectID"];
[projectDictionaryArray addObject:tempProjectDictionaryArray];
}
return projectDictionaryArray;
}
所以只是o重申,我想知道A,是我的写法看起来不错? B,你如何从核心数据中获取/读取关系对象。
So just o reiterate, I would like to know A, is my write method look okay? B, how do you fetch/read the relationship objects from core data.
任何帮助将非常感激。
推荐答案
Core Data中的关系不是一个对象,它的属性恰好对应于模型中的另一个对象,而不仅仅是一个死胡同。你已经是最大的方式,只要检查你的关系是否确定,只要我看到 - 你需要做的是在你的projectList中添加一行以上
A relationship in Core Data isn't an object, its a property which happens to correspond to another object in your model rather than just being a dead end. You're already most of the way there as far as checking whether your relationships are ok as far as I can see -- what you need to do is add one more line in your projectList
[tempProjectDictionaryArray setObject: project.items forKey:@"items"];
您将添加的对象将是一个NSSet。
the object that you will have added will be an NSSet. You can then check that things are as they should be with a loop like this after you've finished setting things up
NSSet itemsForProject = projectDictionaryArray[someIndex][@"items"]
for (Item* currItem in [itemsForProject allObjects]) {
//access some property of the current item to make sure you have the right ones -- \
description for example
NSLog(@"%@", item.description);
}
这篇关于获取coredata关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!