2
請問我需要一個示例代碼,瞭解如何在一對多關係中保存和獲取數據?這是我的兩個NSManagedObject
子類。我設法保存並獲取一對一的關係。如何在覈心數據中插入和獲取一對多關係實體
請問我需要一個示例代碼,瞭解如何在一對多關係中保存和獲取數據?這是我的兩個NSManagedObject
子類。我設法保存並獲取一對一的關係。如何在覈心數據中插入和獲取一對多關係實體
數據保存代碼
UserDefinedInterests *aAserDefineInterrest1 = [NSEntityDescription insertNewObjectForEntityForName:@"UserDefinedInterests" inManagedObjectContext:context];
aAserDefineInterrest1.name = @"PPP";
aAserDefineInterrest1.categoryId = [NSNumber numberWithInt:168004];
UserDefinedInterests *aAserDefineInterrest2 = [NSEntityDescription insertNewObjectForEntityForName:@"UserDefinedInterests" inManagedObjectContext:context];
aAserDefineInterrest2.name = @"QQQ";
aAserDefineInterrest2.categoryId = [NSNumber numberWithInt:12248004];
UserDefinedInterests *aAserDefineInterrest3 = [NSEntityDescription insertNewObjectForEntityForName:@"UserDefinedInterests" inManagedObjectContext:context];
aAserDefineInterrest3.name = @"RRR";
aAserDefineInterrest3.categoryId = [NSNumber numberWithInt:3534535];
NSMutableSet *userDefineInterests = [NSMutableSet setWithObjects:aAserDefineInterrest, nil];
[aTopic addUserDefinedInterests:userDefineInterests];
數據獲取代碼
// Test listing topic details from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SomeEntity"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (TopicDetails *aTopic in fetchedObjects) {
......
NSSet *pp = aTopic.userDefinedInterests;
for (some *somearray in pp.allObjects) {
}
}