[baseten-users] Setting a relationship to nil [Solved - I think]

Réjean Lamy rejean.lamy at mac.com
Wed May 6 04:51:25 EEST 2009


Hi

My problem seems solved this way (excuse my poor english):

DepartmentView.nib objects binding
-------------------------------------------------

- Before (caused problems when I want to set to nil the manager  
relationship)

DbContextBX <->  DeptsBXAC <-> NSTableView (display Dept's names)
                                            ^
					    |											
				             -> NSArrayController <-> NSTableView (display Dept's  
employees names	

BXDatabaseContext connected to the depts database
BXSynchronizedArrayController named "DeptsBXAC" connected to the db  
context and the Department db table
NSArrayController named "EmployeeList" with Content Set binded to  
DeptsBXAC.selection.employees

NSTableView for departments display,  with column binded to  
DeptsBXAC.arrangedObjects.deptName
NSTableView for employees display, with column binded to  
EmployeeList.arrangedObjects.firstName

- After (seems working ok)

DbContextBX <->  DeptsBXAC <-> NSArrayController <-> NSTableView  
(display Dept's names)
                                          					^			
					    					 |		
							 			 -> NSArrayController <-> NSTableView (display Dept's  
employees names)	


BXDatabaseContext connected to the depts database (no change)
BXSynchronizedArrayController named "DeptsBXAC" connected to the db  
context and the Department db table (no change)
NSArrayController named "DeptList" with  Content Array binded to  
DeptsBXAC.arrangedObjects (addition)
NSArrayController named "EmployeeList" with Content set binded to  
DepList.selection.employees (modification, binded to the new DepList  
instead of DeptsBXAC)

NSTableView for departments display,  with column binded to  
DeptList.arrangedObjects.deptName 9modification, binded to the new  
DeptList instead of DeptsBXAC)
NSTableView for employees display, with column binded to  
EmployeeList.arrangedObjects.firstName (no change)



Summary:

I think my problem originate because my poor understanding of how to  
well design in IB, the connections between BX objects,  
NSArrayControllers and NSTableViews or maybe there is something to fix  
in the BX controllers ?

I observe It seems better not to bind an NSTableView directly to  
BXSynchronizedArrayController, at least  in my case, where I am doing  
some manipulation on relationship (ex: setting manager to nil). I  
first doing this in line with the original Hillegass example with  
CoreData.

My working case involved to add a level of indirection, i.e. bind the  
NSTableViews to NSArrayControllers.
I would like BaseTen developers confirms if I am ok (and, if possible   
explain us why ?... ).

Is somebody experimented similar problem ? Some advises ?

Thanks

Rejean Lamy


Le 09-05-05 à 13:21, Réjean Lamy a écrit :

> Hi !
>
> ... more details about my problem:
>
> I can reproduce the pattern.
>
> My test case:
>
> - I have entered some departments (Dept-A, Dept-B and Dept-C)
> - and  employees (Empl-1, Empl-2, Empl-3)
> - all employees are the Dept-A
>
> Starting the Application everything is fine, I can select Dept-A,  
> all employees are listed ok, and I can select a manager, change it,  
> all ok, no error message in the console.
> Selecting Dept-B or Dept-C displayed empty employees list with no  
> manager, so far so good.
>
> Now, with Dept-A selected, when I set the manager relationship to  
> nil by pressing a button (see my code at the end of this email),
> the manager popup showed "No value", perfect (using Navicat or  
> Postgres admin, I can see the manager relationship to NULL), its ok.
>
> Next, always with Dept-A selected I change the manager popup from  
> "No Value" to an employee say "Empl-1", the change is done, but the  
> following error appear in the console:
>
> 2009-05-05 12:01:37.481 Departments[3240:10b] Cannot remove an  
> observer <NSKeyValueObservance 0x108f870> for the key path  
> "firstName" from <BXDatabaseObject 0x1090310> because it is not  
> registered as an observer.
>
> ... from there, always with Dept-A selected, if I changed the  
> manager from "Empl-1" to say "Empl-2" , now the following error  
> showed in the console:
>
> 2009-05-05 12:11:16.654 Departments[3240:10b] Cannot remove an  
> observer <BXSynchronizedArrayController 0x1077250> for the key path  
> "manager.firstName" from <Department 0x109e3c0>, most likely because  
> the value for the key "manager" has changed without an appropriate  
> KVO notification being sent. Check the KVO-compliance of the  
> Department class.
>
> ... from this point, things goes very bad, if I switch from "Dept-A"  
> to  "Dept-B", I see the "Dept-B" employees table populated with the  
> Dept-A employees !
> (there is no employees in Dept-B) !
> Is the tables changed ? No, I start Postgres Admin or Navicat and  
> see all employees are well in "Dept-A". Dept-B show no employees.
>
> ... now fun things, switching between  Dept-A then Dept-B   
> selection, one time Dept-B showed no employees the second time Dept- 
> B showed all the Dept-A employees. Switching again and again  
> displayed exactly the same pattern. Once on two Dept-B listed the  
> Dept-A employees.
>
> From the error messages showed in the console, something seems going  
> out of sync in BXSynchronizedArrayController.
>
> Again, if I don't set the manager relationship to nil, no problem at  
> all.
> But the time the manager is set to nil... boom !! (no crash but  
> inconsistencies )
>
> Following is the code used  I wrote to set a manager to nil on  
> button click:
>
> ---------------------------------
>
> // DepartmentViewController.h
> #import "ManagingViewController.h"
>
> @interface DepartmentViewController : ManagingViewController {
>   IBOutlet NSArrayController *deptAC;
>   IBOutlet NSTextField *textfield;
> }
>
> - (IBAction) setManagerToNil: (id) sender;
>
> @end
>
> ---------------------------------
>
> // DepartmentViewController.m
> #import "DepartmentViewController.h"
> #import "Department.h"
>
> static NSString *selectedEmployeeContext = @"selectedEmployeeContext";
>
> @implementation DepartmentViewController
>
> // init method is Hillegass unchanged code
> - (id)init
> {
> 	if (![super initWithNibName:@"DepartmentView"
> 						 bundle:nil])
> 		return nil;
> 	[self setTitle:@"Departments"];
>   return self;
> }
>
>
> // I added a Button "Set Manager to nil" in the Department view
> - (IBAction) setManagerToNil: (id) sender
> {
>   NSBeep();
>   NSUInteger index = [deptAC selectionIndex]; 								// the  
> selected department index
>   Department *department = [[deptAC arrangedObjects]  
> objectAtIndex:index]; 	// the selected department object
>
>   [department setManager:nil];   							// !!! Things will goes bad  
> if manager relationship is set to nil :(
>
>  // [department setValue:nil  forKey:@"manager"];  			// alternate  
> way I tried, but with same results
>  // [department setPrimitiveValue:nil  forKey:@"manager"];  	// same  
> results too ...
> }
> @end
>
> ---------------------------------
> //Department.h
> #import <BaseTen/BaseTen.h>
>
> @class Employee;
>
> @interface Department :  BXDatabaseObject  { }
> 	@property (retain) NSString * deptName;
> 	@property (retain) Employee * manager;
> 	@property (retain) NSMutableSet* employees;
> @end
>
> ---------------------------------
> //Department.m
> #import "Department.h"
>
> @implementation Department
> 	@dynamic deptName;
> 	@dynamic manager;
> 	@dynamic employees;
> @end
> ---------------------------------
>
>
> Any ideas ?
>
> Rejean lamy
>
> PS: I used BaseTen 1.6.2 and also experimented with compiling  
> frameworks from svn trunk, but same results.
> PS2: Good work BX staff on trunk, I appreciated now to be able to  
> use the same Xcode model name for the to-many relationship (i.e.  
> "employees")
>
>
> Début du message réexpédié :
>
>> De : Tuukka Norri <tuukka.norri at karppinen.fi>
>> Date : 5 mai 2009 10:50:19 HAE
>> À : BaseTen-Users <baseten-users at lists.basetenframework.org>
>> Objet : Rép : [baseten-users] Setting a relationship to nil
>> Répondre à : BaseTen-Users <baseten-users at lists.basetenframework.org>
>>
>> Réjean Lamy kirjoitti 5.5.2009 kello 15.02:
>>> I would like to know if this is legal to set a relationship to  
>>> "nil" like this:
>>>
>>> [department setManager:nil];
>>
>> Hi!
>> This should be perfectly alright. If you have some new sample code,  
>> please send it if you can, and we'll see if there's a bug.
>> -- 
>> Best regards,
>> Tuukka Norri
>> MK&C
>>
>> _______________________________________________
>> baseten-users mailing list
>> baseten-users at lists.basetenframework.org
>> http://lists.basetenframework.org/mailman/listinfo/baseten-users
>




More information about the baseten-users mailing list