我是否使用Singleton与Realm数据库正确?

8 java singleton android design-patterns realm

我有一个应用程序,用户可以从DialogFragment创建/删除/编辑列表.在我的DialogFragments中有这样的方法之前:MainActivtity.adapter.add(String name, String location, double price);

adapter是我的recyclerView的适配器对象.适配器的类具有我对recyclelerView中的项的创建/删除/编辑方法.如上所示,这就像我所理解的那样,也是一种叫做mehtods的可怕方式.

所以我选择将所有这些CRUD方法放在一个单例类中,并像这样调用这些方法: Service.getInstance().add(...);

这是一种正确的方法,我可以做得更好吗?

这就是我如何创建现在包含我的CRUD方法的单例类,而不是像以前一样将它们放在我的适配器类中.

public class Service {

private static Realm realm;
private static Service service = new Service();

private Service() {
    realm = Realm.getInstance(App.getAppContex());
}

public static Service getInstance(){

    if(service == null){
        service = new Service();
    }
    return service;
}



   public void add(String name, String location, double price) {


    ShopListItem shopListItem = new ShopListItem();

    shopListItem.setName(name);
    shopListItem.setLocation(location);
    shopListItem.setPrice(price);
    shopListItem.setTimestamp(System.currentTimeMillis());
    shopListItem.setIsBought(0);

    realm.beginTransaction();
    realm.copyToRealm(shopListItem);
    realm.commitTransaction();
}


public void removeItem(int position, List<ShopListItem> shopListItems) {

    realm.beginTransaction();
    shopListItems.remove(position);
    realm.commitTransaction();
}
Run Code Online (Sandbox Code Playgroud)

此类仅用于获取全局/应用程序上下文

public class App extends Application {


public static Application sApplication;

public static Application getsApplication(){
    return sApplication;
}

public static Context getAppContex(){

    return getsApplication();
}

@Override
public void onCreate() {
    super.onCreate();
    sApplication = this;
}
}
Run Code Online (Sandbox Code Playgroud)

问题从这里更新:

以下是基于评论建议的新方法: 现在,每当我想在Realm数据库中进行CRUD操作时,我总是从我的realm对象的getDefaultInstance开始,并使用realm.close()完成; 每个CRUD方法都会重现这个过程.

public class Service {

private Realm realm;
private static Service service = new Service();

private Service() {


}

public static Service getInstance(){

    if(service == null){
        service = new Service();
    }
    return service;
}

public void removeItem(int position, List<ShopListItem> shopListItems) {
    //new: realm = Realm.getDefaultInstance();

    realm = Realm.getDefaultInstance();
    realm.beginTransaction();
    shopListItems.remove(position);
    realm.commitTransaction();
    realm.close();
    //new: realm.close();
}
Run Code Online (Sandbox Code Playgroud)

Realm配置现在按照Realm的建议移动到我的Application类.

public class App extends Application {


public static Application sApplication;

public static Application getsApplication(){
    return sApplication;
}

public static Context getAppContex(){

    return getsApplication();
}

@Override
public void onCreate() {
    super.onCreate();
    sApplication = this;
final RealmConfiguration realmConfiguration = new    RealmConfiguration.Builder(App.getAppContex()).build();
    realm.setDefaultConfiguration(realmConfiguration);
}
}
Run Code Online (Sandbox Code Playgroud)

jav*_*vaG 2

您根本不应该将领域与单例一起使用。由于领域是面向线程的。您应该打开新的默认实例并将其关闭。

或者您可以使用 threadId 与领域对象的映射。其中线程是弱引用。