我想知道如何从我的 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、如何从核心数据中获取/读取关系对象。
任何帮助将不胜感激。