void notifyObservers ( ApptentiveNotification notification ) { boolean hasLostReferences = false ; // create a temporary list of observers to avoid concurrent modification errors List < ApptentiveNotificationObserver > temp = new ArrayList <> ( observers . size ( ) ) ; for ( int i = 0 ; i < observers . size ( ) ; ++ i ) { ApptentiveNotificationObserver observer = observers . get ( i ) ; ObserverWeakReference observerRef = ObjectUtils . as ( observer , ObserverWeakReference . class ) ; if ( observerRef == null || ! observerRef . isReferenceLost ( ) ) { temp . add ( observer ) ; } else { hasLostReferences = true ; } } // notify observers for ( int i = 0 ; i < temp . size ( ) ; ++ i ) { try { temp . get ( i ) . onReceiveNotification ( notification ) ; } catch ( Exception e ) { ApptentiveLog . e ( e , "Exception while posting notification: %s" , notification ) ; logException ( e ) ; // TODO: add more context info } } // clean lost references if ( hasLostReferences ) { for ( int i = observers . size ( ) - 1 ; i >= 0 ; -- i ) { final ObserverWeakReference observerRef = ObjectUtils . as ( observers . get ( i ) , ObserverWeakReference . class ) ; if ( observerRef != null && observerRef . isReferenceLost ( ) ) { observers . remove ( i ) ; } } } }
Posts notification to all observers .