so are you suggesting that awakeFromInsertion is the correct place to assign these entities?
That is a more correct place.
I actually assumed that there was an editingContext at this point. And you know what they say about assuming.
My interpretation of “willInsert” is that there is an EC, but the EO has not been inserted into it yet. And EO that is not in an EC is a bomb.
If I put my code in awake from insertion, I then call “editingContext().saveChanges” at the end?
I am NOT a fan of having EOs call saveChanges(). That is possibly OK but not something that I would do. All of this seems like something that belongs in an Controller object.
Doing anything to an EO when it is not in an EC (e.g. Before it is inserted) seems like a Very Bad Idea. EOF is not expecting this.
Chuck
On 2015-02-11, 8:17 AM, "Theodore Petrosky" wrote:
but (and I will test this assumption) isn’t awakeFromInsertion called AFTER the insertion? and willInsert Before?
I need to assign these relationships BEFORE the main EO is inserted. So in my case, I want to relate all the BOOKS that belong to the Show, then assign the PrimaryPlayer to the books.
As I said, I will confirm the order of events with some tests.
The next question is the exception. If two people are marked PrimaryMusician ( which is an operator error ), I want to throw an exception and tell the user, “Sorry, could not assign person to Viola as two people are marked “Primary Player”! (Jane Doe,
Peter Simpson)
On Feb 11, 2015, at 10:49 AM, Samuel Pelletier <
email@hidden> wrote:
Theodore,
I think your code would be in a better place in the awakeFromInsertion instead of willInsert.
awakeFromInsertion is called only once when the EO is created. My understanding is willInsert is called when a EO will be saved the first time by an EOEditingcontext.
Samuel
Le 2015-02-10 à 09:13, Theodore Petrosky <
email@hidden> a écrit :
I am in the process of creating an example d2w app. Basically it manages Shows and the People that play them. the end result is a report for any given week and specific show, who played what book?
So I can create shows, people, instruments. and I am looking at the Events. An event is a performance. It needs a show, and date/time (there could be multiple performances on any given day).
So I create an Event, and I need to pick up all the books that belong to the Show and the people that are the Primary players of the book.
My solution was to use the willInsert() in the EO:
@Override
public void willInsert() {
super.willInsert();
EOQualifier bookForPrimaryPersonQualifier = Person.CURRENT.eq(true);
bookForPrimaryPersonQualifier = ERXQ.and(bookForPrimaryPersonQualifier, Person.PERSON_BOOKS.dot(PersonBook.IS_PRIMARY_PLAYER.eq(true)));
EOQualifier booksAssignedToShowQualifier = Book.SHOW.eq(this.show()); //all the books for this show
NSArray<Book> booksForThisEvent = Book.fetchBooks(editingContext(), booksAssignedToShowQualifier, null);
for (Book aBook: booksForThisEvent) {
bookForPrimaryPersonQualifier = ERXQ.and(bookForPrimaryPersonQualifier, Person.PERSON_BOOKS.dot(PersonBook.BOOK.eq(aBook)));
Person aPerson = Person.fetchPerson(this.editingContext(), bookForPrimaryPersonQualifier);
if (aPerson != null) { //if there is a primary person applied to this book, assign him/her to the EventBook else it's null!
EventBook eo = EventBook.createEventBook(this.show().editingContext(), aBook, this);
eo.setPersonRelationship(aPerson);
} else {
EventBook.createEventBook(this.show().editingContext(), aBook, this);
}
bookForPrimaryPersonQualifier = Person.CURRENT.eq(true); //resetting the qualifier to base case
}
}
Am I stepping on myself doing this in the EO and willInsert?
and
I need help with throwing an exception. if there are two people assigned as the primary player of a given book, of course it blows up.
Person aPerson = Person.fetchPerson(this.editingContext(), bookForPrimaryPersonQualifier);
Assuming that the willInsert() method is okay to do, what’s the correct way to surround this with a try/catch and throw the exception. Obviously, if I just surround it with the try/catch like this:
Person aPerson = null;
try {
aPerson = Person.fetchPerson(this.editingContext(), bookForPrimaryPersonQualifier);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the insertion will succeed assigning no one to the offending book.
I can only hope that willInsert passes muster!
Ted
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Help/Unsubscribe/Update your Subscription:
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Help/Unsubscribe/Update your Subscription:
Do not post admin requests to the list. They will be ignored.