Uploaded image for project: 'Realm Java SDK'
  1. Realm Java SDK
  2. RJAVA-170

Old/New values pairs for Single Object Notifications

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      This is for following enhancement of #4101 which are not implemented in #4331

      Cocoa API will notify user when the object changes with old/new value pair:

      @interface RLMPropertyChange : NSObject
      
      /**
       The name of the property which changed.
       */
      @property (nonatomic, readonly, strong) NSString *name;
      
      /**
       The value of the property before the change occurred. This will always be `nil`
       if the change happened on the same thread as the notification and for `RLMArray`
       properties.
      
       For object properties this will give the object which was previously linked to,
       but that object will have its new values and not the values it had before the
       changes. This means that `previousValue` may be a deleted object, and you will
       need to check `invalidated` before accessing any of its properties.
       */
      @property (nonatomic, readonly, strong, nullable) id previousValue;
      
      /**
       The value of the property after the change occurred. This will always be `nil`
       for `RLMArray` properties.
       */
      @property (nonatomic, readonly, strong, nullable) id value;
      @end
      
      

      By extending current java interface ObjectChangeSet, realm-java could have something similar.

      The proposed API 1:

      public interface ObjectChangeset {
        boolean isDeleted();
        String[] getChangedField();
        isFieldChanged(String fieldName);
       
        public static class FieldChange<T> {
          T oldValue;
          T newValue;
        }
      
        // This will always return null for RealmList field.
        <E> FieldChange<E> getChange(E type, String fieldName);
      }
      

      The proposed API 2:

      public interface ObjectChangeset {
        boolean isDeleted();
        String[] getChangedField();
        isFieldChanged(String fieldName);
       
        Long getOldLongValue(String fieldName);
        Long getNewLongValue(String fieldName);
        Date getOldDateValue(String fieldName);
        Date getNewDateValue(String fieldName);
        // ... Other getters
      }
      

      Potential problems & limitations:

      • [ ] Storing old values requires memory and potentially both memory & cpu consuming operation since there might be big strings needed to be converted from StringData to Java String. We may want to give user an option to disable the old/new values in the notification. Something like RealmObject.addChangeListener(RealmObjectChangeListener listener, boolean needValues).
      • [ ] Same like cocoa, the value of the property before the change occurred. This will always be nil
        if the change happened on the same thread as the notification.
      • [ ] All primitive types will be returned as boxed type. This is because of we have to use generic for FieldChange<T>.
      • [ ] short/byte/int/long values will always be reported as FieldChange<Long> since they are all stored as long underneath. From this perspective, the API 2 might be safer for users.

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: