* @return the corresponding ERTag (or null if not found)
*/
@SuppressWarnings( { "cast", "unchecked" })
public ERTag fetchTagNamed(EOEditingContext editingContext, String tagName, boolean createIfMissing) {
NSArray<ERTag> tags = (NSArray<ERTag>) ERXEOControlUtilities.objectsWithQualifier(editingContext, _tagEntity.name(), ERTag.NAME.is(tagName), null, true, true, true, true);
ERTag tag;
if (tags.count() == 0) {
if (createIfMissing) {
// Create it in another transaction so we can catch the dupe exception. Note that
// this means that tags will ALWAYS be created even if the parent transaction
// rolls back. It's mostly for your own good :)
EOEditingContext newEditingContext = ERXEC.newEditingContext();
try {
ERTag newTag = createTagNamed(newEditingContext, tagName);
newEditingContext.saveChanges();
tag = newTag.localInstanceIn(editingContext);
}
catch (EOGeneralAdaptorException e) {
// We'll assume this was because of a duplicate key exception and just retry the original
// fetch WITHOUT createIfMissing. If that returns a null, then we know it was some other
// crazy exception and just throw it.