Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERXEnterpriseObjectCache fix #663

Merged
merged 2 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import er.extensions.eof.ERXDatabaseContextMulticastingDelegate;
import er.extensions.eof.ERXEC;
import er.extensions.eof.ERXEOAccessUtilities;
import er.extensions.eof.ERXEnterpriseObjectCache;
import er.extensions.eof.ERXEntityClassDescription;
import er.extensions.eof.ERXGenericRecord;
import er.extensions.eof.ERXModelGroup;
Expand Down Expand Up @@ -294,7 +295,13 @@ public void finishInitialization() {
}
ERXObjectStoreCoordinatorPool.initializeIfNecessary();
}


@Override
public void didFinishInitialization() {
ERXEnterpriseObjectCache.setApplicationDidFinishInitialization(true);
super.didFinishInitialization();
}

private static Map<String, Support> _qualifierKeys;

public static synchronized void registerSQLSupportForSelector(NSSelector selector, EOQualifierSQLGeneration.Support support) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.webobjects.foundation.NSNotificationCenter;
import com.webobjects.foundation.NSSelector;

import er.extensions.appserver.ERXApplication;
import er.extensions.ERXExtensions;
import er.extensions.foundation.ERXExpiringCache;
import er.extensions.foundation.ERXSelectorUtilities;

Expand Down Expand Up @@ -88,9 +88,10 @@ public class ERXEnterpriseObjectCache<T extends EOEnterpriseObject> {
private boolean _returnUnsavedObjects;

/** If <code>false</code>, the cache is not allowed to fetch values as migrations may not have been processed yet.
* @see ERXApplication#ApplicationDidFinishInitializationNotification
* @see ERXExtensions#didFinishInitialization()
* @see #setApplicationDidFinishInitialization(boolean)
*/
private boolean _applicationDidFinishInitialization;
private static boolean _applicationDidFinishInitialization;

/**
* Creates the cache for the given entity name and the given keypath. No
Expand Down Expand Up @@ -159,7 +160,6 @@ public ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier q
_qualifier = qualifier;
_resetOnChange = true; // MS: for backwards compatibility
_fetchInitialValues = true; // MS: for backwards compatibility
_applicationDidFinishInitialization = false;
start();
}

Expand Down Expand Up @@ -205,7 +205,6 @@ public ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier q
_timeout = timeout;
_qualifier = qualifier;
_returnUnsavedObjects = shouldReturnUnsavedObjects;
_applicationDidFinishInitialization = false;
setRetainObjects(shouldRetainObjects);
setResetOnChange(false);
setFetchInitialValues(shouldFetchInitialValues);
Expand All @@ -218,13 +217,8 @@ public ERXEnterpriseObjectCache(String entityName, String keyPath, EOQualifier q
* @see #stop()
*/
public void start() {
// Catch this to disable caching before application did finish to start (and most importantly processed migrations)
NSSelector selector = ERXSelectorUtilities.notificationSelector("enableFetchingOfInitialValues");
NSNotificationCenter.defaultCenter().addObserver(this, selector,
ERXApplication.ApplicationDidFinishInitializationNotification, null);

// Catch this to update the cache when an object is changed
selector = ERXSelectorUtilities.notificationSelector("editingContextDidSaveChanges");
NSSelector selector = ERXSelectorUtilities.notificationSelector("editingContextDidSaveChanges");
NSNotificationCenter.defaultCenter().addObserver(this, selector,
EOEditingContext.EditingContextDidSaveChangesNotification, null);

Expand All @@ -239,16 +233,25 @@ public void start() {
}

/**
* Call this to stop cache updating.
* Call this to stop cache updating.
* @see #start()
*/
public void stop() {
NSNotificationCenter.defaultCenter().removeObserver(this, ERXApplication.ApplicationDidFinishInitializationNotification, null);
NSNotificationCenter.defaultCenter().removeObserver(this, EOEditingContext.EditingContextDidSaveChangesNotification, null);
NSNotificationCenter.defaultCenter().removeObserver(this, ERXEnterpriseObjectCache.ClearCachesNotification, null);
_cache.stopBackgroundExpiration();
}


/**
* Called from {@link ERXExtensions#finishInitialization()} to enable fetches. This is to ensure that
* migrations have run prior first fetch from this class.
*
* @param didFinish indicator if application did finish initialization phase
*/
public static void setApplicationDidFinishInitialization(boolean didFinish) {
_applicationDidFinishInitialization = didFinish;
}

/**
* Returns the editing context that holds object that are in this cache. If _reuseEditingContext is false,
* a new editing context instance is returned each time. The returned editing context is autolocking.
Expand Down Expand Up @@ -277,7 +280,7 @@ protected ERXEC editingContext() {
* Helper to check a dictionary of objects from an EOF notification and return any that are for the
* entity that we are caching.
*
* @param dict dictionary of key to NSArray<EOEnterpriseObject>
* @param dict dictionary of key to {@literal NSArray<EOEnterpriseObject>}
* @param key key into dict indicating which list to process
* @return objects from the list that are of the entity we are caching, or an empty array if there are no matches
*/
Expand All @@ -298,7 +301,7 @@ private NSArray<T> relevantChanges(NSDictionary dict, String key) {

/**
* Handler for the editingContextDidSaveChanges notification. If <code>_resetOnChange</code> is <code>true</code>, this
* calls reset() to discard the entire cache contents if an object of the given entity has been changed.
* calls reset() to discard the entire cache contents if an object of the given entity has been changed.
* If <code>_resetOnChange</code> is <code>false</code>, this updates the cache to reflect the added/changed/removed
* objects.
*
Expand Down Expand Up @@ -338,25 +341,17 @@ public void editingContextDidSaveChanges(NSNotification n) {
/**
* Handler for the clearCaches notification. Calls reset if n.object is the name of the entity we are caching.
* Other code can send this notification if it needs to have this cache discard all of the objects.
* @param n
*
* @see #ClearCachesNotification
*
* @param n NSNotification with an entity name
*/
public void clearCaches(NSNotification n) {
if(n.object() == null || entityName().equals(n.object())) {
reset();
}
}

/**
* Handler for the ApplicationDidFinishInitializationNotification notification. Enables the fetching of initial
* values and such ensure that any migrations have been processed before.
* @param n notification that is fired in ERXApplication.finishInitialization
*/
public void enableFetchingOfInitialValues(NSNotification n) {
_applicationDidFinishInitialization = true;
NSNotificationCenter.defaultCenter().removeObserver(this,
ERXApplication.ApplicationDidFinishInitializationNotification, null);
}


/**
* @return the name of the EOEntity this cache is for
*/
Expand Down