When would a Collection Navigation Property not be initialized? - entity-framework-6

I'm using EF 6.1, Database First. I have Lazy Loading disabled.
If I load an entity without Eager loading its Collection Navigation Properties, would I ever have to worry that these navigation properties were not properly initialized? Should I be able to use entity.collectionnavigationproperty.Add(new entity) without worrying about initializing the collection navigation property first?

I'm not sure how it works for models created without DataBase First, but it seems that all my entities with Collection Navigation Properties are initialized in the constructor with HashSet<>. That's why I have not encountered issues with nulls or having .Add fail.

Related

Custom ViewController objects don't persist through View changes

I have no idea if I'm using the right terminology here, but I'm hoping someone can help - I'm pretty new to iOS Dev and have run into a problem:
I have created a custom class that is used to store an object. It has a bunch of properties and functions, just as an object should. This object is being declared in my ViewController's .h file, and initialised and used throughout the ViewController. The object holds a bunch of information about a test, and takes some measurements in various threads.
The problem I'm facing is that when I load another view (using ECSlidingViewController for the menu) and then return to the view which had the object...it seems to have forgotten the object. The tasks running in the threads are all still running, but that instance of the object seems to be gone.
Is there a way to preserve an instance of the object when changing views so that when I return to the appropriate view, the object is still there and I can still use it?
Thanks!
I'd recommend you to put the objects reference in a transversal class, something like a manager or any other class which you consider appropriate according to your design and most important it needs to be a class that you're completely sure won't be release nor re-created as view controllers are usually when you change from one view controller to the other.
A singleton instance that manages your main logic could be a good option.
Turns out that my menuViewController that was implementing the ECSlidingViewController was recreating the view that contained the object each time I selected the item from the menu. Thanks #rdelmar for pointing this out!
The object was persisting, but in a viewController that was being replaced each time it was selected from the menu and hence the object was unreachable.
I simply implemented this and references to the viewControllers are now being stored in a mutable dictionary and simply recalled when the view is needed, rather than recreating the view. This means that the viewController is being reused and the object is persisting with it.

Breeze (1.4.5) unidirectional one-to-many: navigation collection not populated

I have a model where I have a two entities, a "Parent" and a "Child".
The relationship should be obvious but goes: Parent has zero or more Child entities.
The Parent has a navigation property called "Children" with a collection of Child entities.
The Child does NOT have a navigation property back to the parent. It only has a foreign key leading back to the parent.
When I load these models into my EntityManager, the "Children" navigation property collection is only ever populated with 1 child, even though the given Parent entity in fact have several.
I have tried adding a "Parent" navigation property to the "Child" entity, and if I do this, the "Children" navigation property on the "Parent" entity is populated correctly with all Child entities when loaded in the EntityManager.
The problem is that this is not an option.
If it helps, I am not using the "Knockout" models, but rather the "backingStore" library for Angular model binding.
I have seen the question on the following link, which describes a rather similar issue (although, supposedly fixed and for a much older version of Breeze): Child entities not populated without inverse property

Should I use a protocol or category with associative objects to give my child view controllers properties?

Im building a custom view controller container and im figuring out which to use to give my children view controllers properties, similarly like UINavigationController grabs a view ontroller's title property, and left and right bar button item. What are the pros and cons of each? Note, i have seen the category method used more such as in te excellent View Deck Controller (https://github.com/Inferis/ViewDeck) and a bunch of other components
EDIT
viewDeck and other controllers use associative objects in conjunction with the category to pull this off.
I'd recommend not using a category: there are a number of XCode/LLVM compiler issues with adding libraries/frameworks that use categories to XCode projects (see https://developer.apple.com/library/mac/#qa/qa2006/qa1490.html). Protocols work cleanly for this sort of thing, in my opinion.
I'm not sure that I would use either. I think I would create a base view controller class with the properties, and then have all your child view controllers inherit from this class. Categories can contain methods but not storage, so no ivars or properties (that have a backing ivar).

Why I can access Core Data entities in viewDidAppear, but not in viewDidLoad?

I have a UITableViewController subclass and when I try to fetch some entities from viewDidLoad, I get an empty array. But when I use the same code in viewDidAppear I get those entities! How's that?
Are you using a NSFetchedResultsController? That is the recommended (and easiest) way to populate a table view with Core Data. You would not have to care about the time of the fetch because the results controller would managed that for you.
Without the FRC you are responsible for populating the table. Depending on your setup, i.e. how you create the view controller, it is possible that all necessary parts (including the managed object context, or a data array, or your table view itself) are not fully loaded yet by the time viewDidLoad runs. Typically, viewDidLoad is best used for view controllers that are loaded from a nib or storyboard. You provide the necessary properties in prepareForSegue: or some such method, and they will be available to viewDidLoad:.
Still, you should go with the FRC. It will do the fetch more or less lazily through the datasource methods. That would also be the best option for performance and memory management.

Table Per Hierarchy based on fields in the Navigation Property

I have an entity which has a navigation property. I would like to define derived entities from the base entity using a field in the navigation property as the discriminator. Is this possible. I could not find a way to do this in the designer.
No it is not possible with navigation property. It should be possible to map two tables in 1:1 relation into single entity. You can try to combine it with table per hiearchy mapping. I'm interested if it works but i don't have chance to try it now.

Resources