Skip to content

Commit

Permalink
Merge pull request #342 from smarthealth/hotfix-erxec
Browse files Browse the repository at this point in the history
Attempt to fix issues with disappearing snapshots when using nested ecs
  • Loading branch information
darkv committed Jan 29, 2013
2 parents 0f9d473 + bed35de commit 1ae0143
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public class ERXEC extends EOEditingContext {
private static final NSSelector EditingContextWillRevertObjectsDelegateSelector = new NSSelector("editingContextWillRevertObjects", new Class[] { EOEditingContext.class, NSArray.class, NSArray.class, NSArray.class });
private static final NSSelector EditingContextDidRevertObjectsDelegateSelector = new NSSelector("editingContextDidRevertObjects", new Class[] { EOEditingContext.class, NSArray.class, NSArray.class, NSArray.class });
private static final NSSelector EditingContextDidFailSaveChangesDelegateSelector = new NSSelector("editingContextDidFailSaveChanges", new Class[] { EOEditingContext.class, EOGeneralAdaptorException.class });

private static final String ERXECProcessQueuedNotificationsNotification = "processQueuedNotifications";
public static final NSSelector ERXECProcessQueuedNotificationsSelector = ERXSelectorUtilities.notificationSelector("processQueuedNotificationsNotification");

/**
* @return the value of the <code>er.extensions.ERXEC.editingContextClassName</code> property, which
Expand Down Expand Up @@ -1483,7 +1486,20 @@ public Object invokeRemoteMethod(EOEditingContext eoeditingcontext, EOGlobalID e
@Override
public void _objectsChangedInStore(NSNotification nsnotification) {
ERXEnterpriseObject.FlushCachesProcessor.perform(this, (NSArray) nsnotification.userInfo().objectForKey("objects"));
if (savingChanges) {

/*
* Check to see if this context, or any parent context, is saving changes.
* If so, queue up the notifications.
*/
boolean isSavingChanges = savingChanges;
EOObjectStore parent = parentObjectStore();
while(!isSavingChanges && parent instanceof ERXEC) {
ERXEC parentEc = (ERXEC) parent;
isSavingChanges = parentEc.savingChanges;
parent = parentEc.parentObjectStore();
}

if (isSavingChanges) {
synchronized (queuedNotifications) {
queuedNotifications.addObject(nsnotification);
}
Expand Down Expand Up @@ -1566,6 +1582,11 @@ private void processQueuedNotifications() {
for (NSNotification notification : queuedNotificationsClone) {
_objectsChangedInStore(notification);
}
NSNotificationCenter.defaultCenter().postNotification(ERXECProcessQueuedNotificationsNotification, this);
}

public void processQueuedNotificationsNotification(NSNotification n) {
processQueuedNotifications();
}

/**
Expand Down Expand Up @@ -1731,6 +1752,10 @@ public EOEditingContext _newEditingContext(EOObjectStore objectStore, boolean va
ec.unlock();
}
NSNotificationCenter.defaultCenter().postNotification(EditingContextDidCreateNotification, ec);
if(objectStore instanceof ERXEC) {
ERXEC parent = (ERXEC)objectStore;
NSNotificationCenter.defaultCenter().addObserver(ec, ERXECProcessQueuedNotificationsSelector, ERXECProcessQueuedNotificationsNotification, parent);
}
return ec;
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/ERXTest/Sources/er/extensions/eof/ERXECTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import er.erxtest.ERXTestCase;
import er.erxtest.ERXTestUtilities;
import er.erxtest.model.Company;
import er.erxtest.model.Employee;

public class ERXECTest extends ERXTestCase {

Expand Down Expand Up @@ -52,6 +53,30 @@ public void testConstructorWithObjectStore() {
Assert.assertEquals(parentEC3, ec3.parentObjectStore());
}

public void testNestedECs() {
try {
EOEditingContext ec = ERXEC.newEditingContext();
Company c = (Company) EOUtilities.createAndInsertInstance(ec, Company.ENTITY_NAME);
c.setName("Name");
ec.saveChanges();
EOEditingContext nested = ERXEC.newEditingContext(ec);
Company nestC = c.localInstanceIn(nested);
Employee e = (Employee) EOUtilities.createAndInsertInstance(nested, Employee.ENTITY_NAME);
e.setFirstName("First");
e.setLastName("Last");
e.setManager(Boolean.FALSE);
e.addObjectToBothSidesOfRelationshipWithKey(nestC, Employee.COMPANY_KEY);
nested.saveChanges();
ec.saveChanges();
System.gc();
c.delete();
ec.saveChanges();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

public void testSerializablilty() throws IOException, ClassNotFoundException {
EOEditingContext ec = ERXEC.newEditingContext();
Company.createCompany(ec, "Some fruit company");
Expand Down

0 comments on commit 1ae0143

Please sign in to comment.