0

我想知道如何从我的 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,我的 write 方法看起来好吗?B、如何从核心数据中获取/读取关系对象。

任何帮助将不胜感激。

4

1 回答 1

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);
}
于 2013-09-29T01:57:29.807 回答