Is it possible to get Core Data cascade delete to delete objects more than one level deep?
ObjectA has an NSSet of ObjectB, ObjectB has an NSSet of ObjectC
When I delete ObjectA, I want ObjectB and ObjectC to be deleted.
I set the relationship of ObjectA<->>ObjectB to cascade, and the relationship of ObjhectB<->>ObjectC to cascade but no dice. Any ideas?
The way you described it, these are one-way relationships. So this answer reflects that case. I think that if you set the relationships like this
object a <-->> object b <-->> object c
(I use <-->> to illustrate one-to-many and many-to-one, since you describe the
relationship of a to b where b is a set.)
you will have object b depending on the existence of object a and object c depending on the existence of object b.
Then if object a disappears, then object b sees that the relationship is not fulfilled and will delete itself. Then object c will see that the relationship is not fulfilled and delete itself.
As it is written in your question, object b does not have a relationship to object a, so it is not expecting anything with respect to object a.
Related
In the book, Eric Evans shows an example where an VALUE OBJECT holds ENTITIES. VALUE OBJECTS are immutable, ENTITIES not.
The question is: If an ENTITY that
is referenced from an VALUE OBJECT change its state, was the immutability been broken?
In my opinion it doesn't break the immutability, because the "value" of the
object lies on the ENTITIES array, not on their states.
What do you guys think?
It depends if you can mutate the entities directly through the VO or not.
Imagine a VO instance that can be shared between multiple objects. If the VO contains a mutable entity and exposes it as a public member, multiple clients may update the entity concurrently. This leads to problems you would expect not to happen with actual immutability.
If the VO only holds the ID of the referenced entity on the other hand, you would have to fetch a brand new instance from a Repository before changing it, avoiding the shared mutable state issue.
I have a composite relationship between two objects (A & B) (A is composed of many Bs). Now another class (C) has a one-to-many association relationship to class 'B'. I would like to be able to retrieve all instances of class (A) from class (C).
How do I do this without creating redundant associations? Since 'C' has basically a list of 'Bs' I can't just iterate over all of them asking what's your 'A' and eventually returning a list of 'A' to 'C'.
I really hope someone out there understands this and doesn't find it completely confusing!
Thanks
Update:
Dataset has a list of defined variables. An activity can select a subset of variables from each dataset and give some attributes to them, hence an association class is used. Now if I want to be able to retrieve from an Activity instance the datasets it is registered with, how do I achieve this in UML and in object implementation?
According to your task, it is IMPOSSIBLE to take all B's from all C's. Because there is no sentence that states that any B belong to some C.
On the contrary, as A have compositions of B (notice, A IS NOT composition, A HAS composition of B, for A can have heaps of other things, too), and any B MUST belong to some A object, you can easily get all B's from all A's. Only create the list of B as a set for not to have multiply values.
But even if the association B-A includes B->A connection, you cannot get all A's from B's. Because some A's can be EMPTY. You'll never reach them. from B's.
So, you cannot take all A from C's for TWO important reasons. And NO redundant association will help.
As for the question set after "Update",
For getting All from variables, use
Dataset <---- Variable ---> Activity // This variant is the easiest for adding associations.
For getting connected datasets from an activity,
Dataset <--- Variable <----- Activity
But please, notice, it is not updated, it is DIFFERENT question.
I assume your diagram would look something like this :
If C has a reference to B, and B has a reference to A, then it should be no problem navigating to A from C. There is no need for any additional redundant relationships.
In my CoreData app, all inverse many-to-many relationships have a relationship fault. -- observed using NSLog(#"%#", [self description]) . The fault appears after data is saved and subsequently opened.
The relationship delete rule might be the culprit.
Here is an example.
Person
friends<<--->> (An inverse many-to-many relationship on self.
addresses<<--| (A many-many relationship to Address
|
Address |
owners<<-----| (A many-many relationship to person.address Addresses can be shared
The Address.owners relationship is set to deny.
The Person.addresses relationship is set to nullify.
The Person.friends relationship is set to deny.
I'd appreciate some clarification:
Given an easier case, where address is NOT shared.
AddressBoook Person Address
people-------->addresses---->>owner
Person.address is set to Nullify. (Remove the address(es) only when the owner is removed)
the Address.owner is set to .... No action. (Dont want to delete the person. Or nullify? Because Person is still held by AddressBook object
What are we deleting, the relationship or the owning object.
In a many-many relationship, can we consider, Nullify to occur on the removal of the last object -- or the first one and thus dangling those others in space. It must be the deletion of the Set!.
Thanks
I have a SQLCe database which had a unique primary key Id with type uniqueidentifier, and child relationship, 1 to many, where i keep the master id in a column and append unique id for every row too. Now I use POCO entities for my domain model NOT STE. While adding and modifying entities works OK, I have hard time to delete, say for now individual child records, where of course supposed that they have they're own primary key. Soon as i give the deleted list and iterate through each entity while first entity is attached, in the second i get the exception:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
I should mention that i first make any add and modify entities to the database while opening a context...disposing and call another method for deletion where of course opens another context and if it ends successfully disposing.
Whats the meaning for this exception that I don't get?
This exception occurs when you have two different instances of the same entity in your object graph. This exception also occurs in STE but there is a work-around for that.
Basically, you have two entities (POCO), each with a relationship to a third entity. When you try to associate the first entity with the second and try to persist it into the Entity Context, both entities will have a seperate instance of the third and this is where the error is.
Work-around:
Before you make the association between two entities, try to "merge" any related entities together. This is so that the Entity Context does not need to make the decision of which instance of the "common entity" to persist.
Example:
A, (B1,B2), C are entities.
A -- B1
C -- B2
A -- B1 -- C
In this scenario, B1 and B2 are both B entities, just different instances (may be due to different calls to the data store through different contexts). When you want to associate A and C, you must choose to drop B2 and link C to B1.
Suppose I have the following data model:
Entity Person
Attribute name String
Attribute personType String
Attribute dailyRecords
Entity CarpenterDailyRecord
Attribute numberOfNailsHammered Int
Attribute picNameOfFinishedCabinet String
Entity WindowWasherDailyRecord
Attribute nameOfBuildingWashed String
Attribute numberOfWindowsWashed Int
I would like to establish a to-many relationship between the Person.dailyRecords and 1 of the daily record entities (which changes depending on the person type). Of course, i could create a CarpenterPerson and WindowWasher entity which each points to it's unique daily record structure, but i have to group people together in my app somehow.
so if i do a Group Entity:
Entity Group
Attribute people array
i'm still stuck. how do i point to multiple & different Person entities?
There must be an obvious answer, it's just i'm so new to all of this. thanks!
Create a parent (DailyRecord) entity that handles the relationship (Person <-->> DailyRecord). [CarpenterDailyRecord|WindowWasherDailyRecord] then inherits from DailyRecord.
The risk with this, however, is that all of the children (WindowWasherDailyRecord, CarpenterDailyRecord) will be in one table in the underlying sqlite structure and therefore can cause a performance impact. This is not a reason to avoid inheritance, just something to be aware of while designing your data model.