所以,我有一个`NSManagedObject 的用户、边界和预设。预设始终与单个用户相关联。一个用户可以有许多预设。每个预设都可以绑定一个边界。
基本上,用户有一个预设,他可以保存到他拥有的每个边界。
用户与预设有一对多的关系。Boundary 与 Preset 具有多对多的关系。
我正在尝试生成用户拥有的预设列表减去已经绑定到边界的预设列表。
我正在使用魔法唱片。
我的问题是当我创建一个新的用户和边界时,这有效:
Boundary *boundary = [Boundary MR_createEntity];
boundary.name = @"test boundary";
UserDB *user = [UserDB MR_createEntity];
user.username = @"test User";
Preset *preset01 = [Preset MR_findFirstByAttribute:@"nameDisplay" withValue:@"C4"];
DLog(@"preset01.nameDisplay: %@", preset01.nameDisplay);
Preset *preset02 = [Preset MR_findFirstByAttribute:@"nameDisplay" withValue:@"B"];
DLog(@"preset02.nameDisplay: %@", preset02.nameDisplay);
[boundary setPresets:[NSSet setWithObject:preset01]];
[user setPresets:[NSSet setWithObjects:preset01, preset02, nil]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@ AND boundary != %@", user, boundary];
NSArray *presetsList = [Preset MR_findAllWithPredicate:predicate];
DLog(@"presetsList: %@", presetsList);
所以我将 preset01 设置为边界。我为用户设置了 preset01 和 preset02。所以我需要一个向用户显示preset02 的列表(因为preset01 已经绑定到边界,用户应该不能再次添加它)。
DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
"<SoilTestPointPreset: 0x1e06b8b0> (entity: Preset; id: 0x1e06bca0 <x-coredata://7476DB86-AF79-445C-B3AE-6C91088704A0/Preset/p98> ; data: {\n attributes = \"<relationship fault: 0x1e074000 'attributes'>\";\n boundary = nil;\n gpsLocation = nil;\n nameDisplay = B;\n nameTitle = nil;\n rgbColor = \"0x1e06b1d0 <x-coredata://7476DB86-AF79-445C-B3AE-6C91088704A0/RGBColor/p49>\";\n testing = nil;\n user = \"0x1e05ff60 <x-coredata:///UserDB/t59757D99-2FBF-4FBF-97AF-39582FC4B5503>\";\n})"
)
这就是我的预期。但是现在当我获取用户和边界对象时:
Boundary *boundary = [Boundary MR_findFirstByAttribute:@"boundaryID" withValue:@3748];
DLog(@"boundary.name: %@", boundary.name);
UserDB *user = [UserDB MR_findFirstByAttribute:@"uid" withValue:@99];
DLog(@"user.username: %@", user.username);
我的数组是空的:
DEBUG | -[LoginViewController viewDidLoad] | boundary.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | user.username: thatPerson
DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
)
为什么从核心数据中获取用户和边界会改变结果而不是创建结果?
更新:
我补充说:
Boundary *boundary = [Boundary MR_findFirstByAttribute:@"boundaryID" withValue:@3748];
DLog(@"boundary.name: %@", boundary.name);
DLog(@"boundary.presets.count: %d", boundary.presets.count); // Added
UserDB *user = [UserDB MR_findFirstByAttribute:AVI_UID withValue:@99];
DLog(@"user.username: %@", user.username);
DLog(@"user.presets.count: %d", user.presets.count); // Added
DLog(@"AFTER | boundary.presets.count: %d", boundary.presets.count); //Added
DLog(@"AFTER | user.presets.count: %d", user.presets.count); //Added
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait]; //Added
他们 User 和 Boundary 有 Presets 关系:
DEBUG | -[LoginViewController viewDidLoad] | boundary.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | boundary.presets.count: 0
DEBUG | -[LoginViewController viewDidLoad] | user.username: iDealer
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | user.presets.count: 0
DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | AFTER | boundary.presets.count: 1
DEBUG | -[LoginViewController viewDidLoad] | AFTER | user.presets.count: 2
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Saving <NSManagedObjectContext (0x20831a80): *** DEFAULT ***> on *** MAIN THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Save Parents? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Save Synchronously? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Saving <NSManagedObjectContext (0x1f59e860): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Save Parents? 0
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Save Synchronously? 1
__70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x1f59e860) → Finished saving: <NSManagedObjectContext (0x1f59e860): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
)