SingleLiveEvent理解

本文深入解析SingleLiveEvent类,一种在Android开发中用于LiveData的特殊实现。它通过AtomicBoolean的compareAndSet方法确保观察者在数据变化时仅响应一次,避免重复触发onChanged方法,有效提升应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       利用SingleLiveEvent 使 observe#LiveData时只相应一次onChanged操作
 SingleLiveEvent 利用 AtomicBoolean (默认为false)进行赋值,当LiveData 进行 setValue时
 改变 AtomicBoolean的值(set(true))使用 AtomicBoolean.compareAndSet(true,false)方法,
 先进行判断(将set(true)的值与 compareAndSet设置的except值(第一个参数)比较,因为相等所以
 将AtomicBoolean值设为false(compa..第二个参数的值) 返回 true ,);当再次进入该页面虽然 LiveData值
 并没有改变,仍然触发了 observer方法,但是 由于 AtomicBoolean已经为 false ,但是 except值为 true ,
 与if 进行判断所以 并不会继续触发 onChanged(T)方法
 即只有在 setValue时相应一次onChanged(T)方法。

源码如下:

public class SingleLiveEvent<T> extends MutableLiveData<T> {

    private static final String TAG = "SingleLiveEvent";

    private final AtomicBoolean mPending = new AtomicBoolean(false);

    @MainThread
    public void observe(LifecycleOwner owner, final Observer<? super T> observer) {

        if (hasActiveObservers()) {
            Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
        }

        // Observe the internal MutableLiveData
        super.observe(owner, new Observer<T>() {
            @Override
            public void onChanged(@Nullable T t) {
                if (mPending.compareAndSet(true, false)) {  //
                    observer.onChanged(t);
                }
            }
        });
    }

    /**
     *  modify by bzb
     *   当 setValue时 mPending.set(true) compareAndSet(true,false) (true(set(true)后的“原值”) == true(except 值))=true  -> mPending.set(false(update值))  所以 再次 observer
     *   时 该值为false 所以不会执行 onChanged(t)方法
     * @param t
     */
    @MainThread
    public void setValue(@Nullable T t) {
        mPending.set(true);
        super.setValue(t);
    }

    /**
     * Used for cases where T is Void, to make calls cleaner.
     */
    @MainThread
    public void call() {
        setValue(null);
    }
}

 

AtomicBoolean的 compareAndSet方法

  /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * @param expect the expected value
     * @param update the new value
     * @return {@code true} if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(boolean expect, boolean update) {
        return U.compareAndSwapInt(this, VALUE,
                                   (expect ? 1 : 0),
                                   (update ? 1 : 0));
    }

 

 

这个解决了什么问题/* * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.czl.lib_base.event import android.util.Log import androidx.annotation.MainThread import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer import java.util.concurrent.atomic.AtomicBoolean /** * A lifecycle-aware observable that sends only new updates after subscription, used for events like * navigation and Snackbar messages. * * * This avoids a common problem with events: on configuration change (like rotation) an update * can be emitted if the observer is active. This LiveData only calls the observable if there's an * explicit call to setValue() or call(). * * * Note that only one observer is going to be notified of changes. */ open class SingleLiveEvent<T> : MutableLiveData<T>() { private val mPending = AtomicBoolean(false) @MainThread override fun observe(owner: LifecycleOwner, observer: Observer<in T>) { if (hasActiveObservers()) { Log.w(TAG, "Multiple observers registered but only one will be notified of changes.") } // Observe the internal MutableLiveData super.observe(owner) { t -> if (mPending.compareAndSet(true, false)) { observer.onChanged(t) } } } @MainThread override fun setValue(t: T?) { mPending.set(true) super.setValue(t) } /** * Used for cases where T is Void, to make calls cleaner. */ @MainThread fun call() { value = null } companion object { private const val TAG = "SingleLiveEvent" } }
最新发布
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值