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.
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: