我将Singleton与Realm Database正确使用了吗? [英] Have I used Singleton with Realm Database correct?

查看:75
本文介绍了我将Singleton与Realm Database正确使用了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app where the user can create/delete/edit to a list from a DialogFragment. Before I had a method like this in my DialogFragments: MainActivtity.adapter.add(String name, String location, double price);

adapter是我的recyclerView适配器对象.适配器的类具有recyclerView中各项的我的create/delete/edit方法.据我所知,它的调用方式如上图所示,这也是一种可怕的称呼方法.

adapter is my adapter object for the recyclerView. The class for the adapter had my create/delete/edit methods for the items in the recyclerView. Which was called like shown above which also is a horrible way to call mehtods as I understand.

因此,我选择将所有这些CRUD方法放在一个单例类中,并按如下方式调用这些方法:Service.getInstance().add(...);

So I choose to put all these CRUD methods in a one singleton class and call these method like this: Service.getInstance().add(...);

这是正确的方法吗,我能做得更好吗?

Is this a correct approach and what could I have done better?

这就是我制作现在包含CRUD方法的单例类的方法,而不是像以前那样将它们放入recyclerView的适配器类中.

This is how I made the singleton class that now contains my CRUD methods, instead of putting them in my adapter class for the recyclerView as before.

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();
}

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

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;
}
}

问题从此处更新:

Question updated from here:

以下是基于评论建议的新方法: 现在,每次我想在Realm数据库中进行CRUD操作时,我总是从我的领域对象的getDefaultInstance开始,并以realm.close()结尾.此过程在每种CRUD方法中都可以重用.

Here is the new approach based on suggestions from comments: Now everytime I want to make a CRUD operation in the Realm Database, I always start with the getDefaultInstance for my realm object and finish of with realm.close(); this procces is reapted in every CRUD method.

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();
}

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);
}
}

推荐答案

您根本不应该将单身人士用于领域.由于领域是面向线程的.您应该打开新的默认实例并关闭它.

You should not be using realm with singleton at all. As realm is thread oriented. you should open new default instance and close it.

或者您可以使用threadId与realm对象的映射.线程是弱引用的地方.

Or you can use a map of threadId vs realm object. where thread is weak reference.

这篇关于我将Singleton与Realm Database正确使用了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆