简体   繁体   English

同步适配器类中的singelton对象

[英]singelton object in sync adapter class

I need help regarding the explanation of 我需要有关解释的帮助

why do we use a singleton object in sync adapter service class 为什么我们在同步适配器服务类中使用单例对象

private static SyncAdapter sSyncAdapter = null;
private static final Object sSyncAdapterLock = new Object();

    @Override
    public void onCreate() {
        synchronized (sSyncAdapterLock) {
            if (sSyncAdapter == null) {
                sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
            }
        }
    }

Because the Framework is build in the way to work with only one SyncAdapter: 因为框架是通过仅与一个SyncAdapter一起工作的方式构建的,所以:

The sync adapter framework is designed to work with sync adapter components that are singleton instances. 同步适配器框架旨在与作为单例实例的同​​步适配器组件一起使用。

You can have multiple component triggering the SyncAdapter to send some data. 您可以有多个组件触发SyncAdapter发送一些数据。 But you only want to create ONE SyncAdapter to manage all the calls. 但是,您只想创建一个SyncAdapter来管理所有呼叫。 Therefore you create the SynAdapter with the singleton-pattern. 因此,您将使用单例模式创建SynAdapter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM