• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Code review for refreshToOneRelationshipWithKey
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Code review for refreshToOneRelationshipWithKey


  • Subject: Re: Code review for refreshToOneRelationshipWithKey
  • From: Ricardo Parada <email@hidden>
  • Date: Sat, 03 Sep 2016 19:15:32 -0400

Thanks Aaron, your suggestion is working well.  It is analogous to a "git rebase” ;-)  I feel better with this code.

I did find that the technique did not work for to-many relationships, but added a bit of code towards the end of the method to refresh the to-many relationships using Wonder:

    /**
     * Core method to refresh properties selectively with data from the database. The
     * properties can be attributes, to-one or to-many relationships.
     */
    public void refreshProperties(NSArray<String> keys) {
        NSDictionary<String, Object> changes = changesFromCommittedSnapshot();


        // Discard changes to the properties that we're about to refresh if any
        NSMutableDictionary<String, Object> changesToKeepAfterRefresh = changes.mutableClone();
        changesToKeepAfterRefresh.removeObjectsForKeys(keys);


        // Now fetch the receiver refreshing it with changes from database
        EOEntity entity = entity();
        EOFetchSpecification fs = new EOFetchSpecification(entity.name(), primaryKeyQualifier(), null);
        fs.setRefreshesRefetchedObjects(true);
        editingContext().objectsWithFetchSpecification(fs);


        // Now re-apply the changes we want to keep
        if (changesToKeepAfterRefresh.count() > 0) {
            takeValuesFromDictionary(changesToKeepAfterRefresh);
        }


        // The above will not refresh to-many relationships so do something a bit
        // more drastic to refresh the to-many relationships.
        for (String key : keys) {
            EORelationship relationship = entity.relationshipNamed(key);
            if (relationship.isToMany()) {
                ERXEOControlUtilities.clearSnapshotForRelationshipNamed(this, key);
            }
        }
    }

And some convenient methods to go with it too:

    /**
     * Convenience method to refresh properties selectively with data from the database.
     * The properties can be attributes, to-one or to-many relationships.
     */
    public void refreshProperties(String... keys) {
        NSArray<String> keysArray = new NSArray<String>(keys);
        refreshProperties(keysArray);
    }


    /**
     * Convenience method to refresh properties selectively with data from the database.
     * The properties can be attributes, to-one or to-many relationships.
     */
    public void refreshProperties(ERXKey<?>... erxKeys) {
        NSMutableArray<String> keys = new NSMutableArray<String>(erxKeys.length);
        for (ERXKey<?> erxKey : erxKeys) {
            keys.add(erxKey.key());
        }
        refreshProperties(keys);
    }




On Sep 3, 2016, at 4:45 PM, Aaron Rosenzweig <email@hidden> wrote:

Hi Ricardo,

That’s some clever WO-ninja magic you got going on there. I’ve never replaced the original snapshot of an EO before.

If it works, great - I won’t knock it but I’d be scared without flexing it (using it a bunch). 

Another idea… 

1) hold onto the “changesFromCommittedSnapshot” so you can refer to it.

2) in your fetchSpec make “setRefereshesRefetchedObjects()” to true - so when you go round trip you’ll update the EO.

3) reapply your changes from the committed snapshot.

Cheers,
Aaron Rosenzweig / Chat 'n Bike
e:  email@hidden  t:  (301) 956-2319
Chat 'n Bike Chat 'n Bike
Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Code review for refreshToOneRelationshipWithKey (From: Ricardo Parada <email@hidden>)

  • Prev by Date: Re: Code review for refreshToOneRelationshipWithKey
  • Next by Date: Re: Code review for refreshToOneRelationshipWithKey
  • Previous by thread: Re: Code review for refreshToOneRelationshipWithKey
  • Next by thread: Deadlocks, editing context locking and network tasks
  • Index(es):
    • Date
    • Thread