获取核心数据关系

时间:2013-09-29 01:34:30

标签: ios objective-c core-data nsfetchrequest

我想知道如何从我的coredata关系船上取物品。我想它应该是一个字典或数组或者返回的东西,这样你就可以得到你的一对多。

我很遗憾,我知道如何写/读单个对象,但这种关系让人感到困惑。

我想我已经成功地写了一个关于coredata的关系但是现在我想能够阅读它以确定我是否正确..我已经开始为此编写方法但不知道实际上要做什么获取所有信息。

这是我到目前为止的代码..对于读取和写入

- (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;
}

所以只是重申一下,我想知道A,我的写法看起来还行吗? B,如何从核心数据中获取/读取关系对象。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Core Data中的关系不是一个对象,它的属性恰好与模型中的另一个对象相对应,而不仅仅是一个死胡同。你已经完成了大部分工作,只要我能看到你的关系是否合适 - 你需要做的是在projectList中添加一行

[tempProjectDictionaryArray setObject: project.items forKey:@"items"];

您将添加的对象将是NSSet。然后,在完成设置后,您可以使用这样的循环检查事物是否正常

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);
}