So, I have a `NSManagedObject's User, Boundary, and Preset. A Preset is always tied to a single user. A user can have many Presets. Each Preset may be tied a boundary.
Basicly, the User has a Preset that he can save to each Boundary he has.
User has a to-many relationship to Preset. Boundary has a many-to-many relationship to Preset.
I am trying to generate a list of Presets that the User has minus the ones already tied to the boundary.
I am using Magical Records.
My issue is when I create a new User and Boundary, this works:
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);
So I set preset01 to the boundary. I set preset01 and preset02 to the user. So I need a list that shows preset02 to the user (since preset01 is already tied to the boundary, the user shouldn't be able to add it again).
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})"
)
Thats what I expected. But now when I fetch the User and Boundary objects:
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);
My array is empty:
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: (
)
Why does fetching the User and Boundary from Core Data change the results as opposed to creating them?
UPDATE:
I added:
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
They User and Boundary have Presets relationships:
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: (
)