我在核心数据中设置了一对多关系。我可以从“主”实体查看/添加/删除对象,但是现在我想查看/添加/删除对象到许多“子”实体,每个主实体应该有单独的子对象列表。
我试过这段代码:
NSManagedObjectContext *context = [[DataSingleton sharedSingleton] thisMain].managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Subs" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
wolArray = [NSMutableArray arrayWithArray:fetchedObjects];
[[DataSingleton sharedSingleton] thisMain] 是我试图获取 Subs 的 Main。我做错了吗?它似乎给了我所有电源的所有 Subs。
现在有可能我将它们添加错了,并且它们没有被添加到 Main 的 Subs 中(如果那可能吗?)
这就是我添加它们的方式:
NSManagedObjectContext *context = [[DataSingleton sharedSingleton] thisMain].managedObjectContext;
Subs *subsInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"Subs"
inManagedObjectContext:context];
subsInfo.name = [newDevice objectForKey:@"name"];
subsInfo.address = [newDevice objectForKey:@"address"];
subsInfo.post = [newDevice objectForKey:@"post"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}