private void UnsubscribeFromNotifications(DictionaryNotificationCallbackDelegate <TValue> callback)
 {
     if (_keyCallbacks.Remove(callback) &&
         _keyCallbacks.Count == 0)
     {
         UnsubscribeFromNotifications();
     }
 }
        public IDisposable SubscribeForKeyNotifications(DictionaryNotificationCallbackDelegate <TValue> callback)
        {
            Argument.NotNull(callback, nameof(callback));

            if (_keyCallbacks.Count == 0)
            {
                SubscribeForNotifications();
            }
            else if (_deliveredInitialKeyNotification)
            {
                callback(this, null, null);
            }

            _keyCallbacks.Add(callback);

            return(NotificationToken.Create(callback, UnsubscribeFromNotifications));
        }
Exemple #3
0
        /// <summary>
        /// A convenience method that casts <see cref="IQueryable{T}"/> to <see cref="IRealmCollection{T}"/> and subscribes for change notifications.
        /// </summary>
        /// <param name="dictionary">The <see cref="IDictionary{String, T}"/> to observe for changes.</param>
        /// <typeparam name="T">Type of the elements in the dictionary.</typeparam>
        /// <seealso cref="IRealmCollection{TValue}.SubscribeForNotifications"/>
        /// <param name="callback">The callback to be invoked with the updated <see cref="IRealmCollection{T}"/>.</param>
        /// <returns>
        /// A subscription token. It must be kept alive for as long as you want to receive change notifications.
        /// To stop receiving notifications, call <see cref="IDisposable.Dispose"/>.
        /// </returns>
        public static IDisposable SubscribeForKeyNotifications <T>(this IDictionary <string, T> dictionary, DictionaryNotificationCallbackDelegate <T> callback)
        {
            Argument.NotNull(dictionary, nameof(dictionary));

            if (dictionary is RealmDictionary <T> collection)
            {
                return(collection.SubscribeForKeyNotifications(callback));
            }

            throw new ArgumentException($"{nameof(dictionary)} must be an instance of IRealmCollection<KeyValuePair<string, {typeof(T).Name}>>.", nameof(dictionary));
        }