Пример #1
0
  public void updateSourceObject(NSArray newValues) {
    Object realSourceObject = realSourceObject();
    String realRelationshipKey = realRelationshipKey();

    newValues = newValues != null ? newValues : NSArray.EmptyArray;

    if (realSourceObject instanceof EOEnterpriseObject
        && // only add/remove if we have an EO and handle a relationship
        ((EOEnterpriseObject) realSourceObject)
                .classDescriptionForDestinationKey(realRelationshipKey)
            != null) {
      EOEnterpriseObject eo = (EOEnterpriseObject) realSourceObject;
      NSArray currentValues = (NSArray) eo.valueForKey(realRelationshipKey);
      currentValues = currentValues != null ? currentValues : NSArray.EmptyArray;

      for (int i = currentValues.count() - 1; i >= 0; i--) {
        EOEnterpriseObject o = (EOEnterpriseObject) currentValues.objectAtIndex(i);
        if (newValues.indexOfIdenticalObject(o) == NSArray.NotFound) { // not found
          eo.removeObjectFromBothSidesOfRelationshipWithKey(o, realRelationshipKey);
        }
      }

      for (int i = newValues.count() - 1; i >= 0; i--) {
        EOEnterpriseObject o = (EOEnterpriseObject) newValues.objectAtIndex(i);
        if (currentValues.indexOfIdenticalObject(o) == NSArray.NotFound) { // not found
          eo.addObjectToBothSidesOfRelationshipWithKey(o, realRelationshipKey);
        }
      }
    } else {
      // NOTE ak: this implementation is different from what JavaWOExtensions do.
      // There, the existing array is fetched and added to/removed from. Here, we simply set the
      // new array. I changed this because it looked like a bad idea to change the array without
      // the sourceObjects's knowledge.
      NSKeyValueCoding.Utility.takeValueForKey(
          realSourceObject,
          (newValues instanceof NSMutableArray) ? newValues : newValues.mutableClone(),
          realRelationshipKey);
    }
  }