Synchronizing the content of multiple NSArrayControllers

As an application grows and it’s UI becomes more complex, you definetly want to split the UI into multiple views and hold each of it in a seperate Nib File. This makes your project a lot more cleaner and helps you to keep overview.

If you’re using Core Data and you want to access your entities from all of your views you need to put an Array Controller in each of your Nib. But now you’re running into the problem of synchronizing the contents of your Array Controllers since you have to provide the complete application data to all the views in which you need it. Fortunatly Cocoa Bindings help you to solve this issue quite elegantly.

Download this sample project which is an excerpt of a project I’m currently working on.

Now, let me explain what this project does in general: Basically you need some

Master Array Controller” which is bound to your applications ManagedObjectContext and actually holds your application’ data. Then you need to bind the arrangedObjects key of the Array Controller(s) which you want to synchronize with the Master Controller to the Master Controllers contentArray. Then, you need to bind the managedObjectContext of your Slave Controller(s) to the app delegate’s managedObjectContext. That’s it! Here’s some code which illustrates this process:

1
2
	[[(MainWindowController*)[self windowController] arrayControllerMaster] bind:@"contentArray" toObject:slaveArrayController withKeyPath:@"arrangedObjects" options:nil];
	[slaveArrayController bind:@"managedObjectContext" toObject:[NSApp delegate] withKeyPath:@"managedObjectContext" options:nil];


This example assumes, that the Master Controller is accessible in some way. In my example, I put it in the MainWindowController class and created a read-only accessor method. Take a look at the sample project to see what I mean.

Conclusion:
As you can see, Cocoa Bindings enable you to synchronize the ArrayController of your application in a very easy manner. This helps you to keep your Nib Files and controller classes clean and well arranged.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Warning: count(): Parameter must be an array or an object that implements Countable in /home/httpd/vhosts/shrdlu.ch/shrdlu.ch/wp-includes/class-wp-comment-query.php on line 405

One Response to Synchronizing the content of multiple NSArrayControllers

  1. Your code appears to have some issues. You are binding the content of the masterArrayController to the slaveArrayController, when it should be bound to an entity in the managedObjectContext. Once the masterArrayController has its content, then you can bind other arrayControllers to it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.