Android,Realm:为什么方法isClosed()返回false?

时间:2017-08-17 07:22:35

标签: android realm

Realm 3.5.0

我在MyApp中添加了Realm。

这里摘录:

public class MainApp extends MultiDexApplication {    
    private void setRealmDefaultSyncConfiguration(SyncUser syncUser, String realmURL) {
        SyncConfiguration sync = new SyncConfiguration.Builder(syncUser, realmURL).build();
            Realm.setDefaultConfiguration(sync );
            if (BuildConfig.DEBUG)
                Log.d(TAG, "setRealmDefaultSyncConfiguration: SUCCESS SET REALM DEFAULT CONFIGURATION:"
                        + "\nThreadSignature = " + AndroidUtil.getThreadSignature()
                        + "\nsyncUser = " + syncUser
                        + "\nrealmURL = " + realmURL
                        + "\nRealm = " + Realm.getDefaultInstance()
                        + "\nRealmget. DefaultConfiguration:\n\n" + Realm.getDefaultInstance().getConfiguration() + "\n\n"
                );
    }
}

这里是logcat数据:

 setRealmDefaultSyncConfiguration: SUCCESS SET REALM DEFAULT CONFIGURATION:
 Thread=main: id=1, priority=5, group=main
 syncUser = {UserId: 32afeeeeeeeeeeeeeeee3c63f5606c4b, AuthUrl: http://172.12.11.90:9080/auth, IsValid: true, Sessions: 0}
 realmURL = realm://172.12.11.90:9080/fccc46ssssssssssssssssb9e828700c/db
 Realm = io.realm.Realm@4225bd68
 Realmget. DefaultConfiguration:  
 realmDirectory: /data/data/com.myproject/files/realm-object-server/32afeeeee/fccc46bbe2354bae35d3ddb9e828700c
 realmFileName : db
 canonicalPath: /data/data/com.myproject/files/realm-object-server/32afeeeee3f5606c4b/fccc46ssssssssssssssssb9e828700c/db
 key: [length: 0]
 schemaVersion: 0
 migration: null
 deleteRealmIfMigrationNeeded: false
 durability: FULL
 schemaMediator: io.realm.DefaultRealmModuleMediator@42136c40
 readOnly: false
 compactOnLaunch: null
 serverUrl: realm://172.12.11.90:9080/fccc46ssssssssssssssssb9e828700c/db
 user: {UserId: 32afeeeeeeeeeeeeeeee3c63f5606c4b,
 AuthUrl: http://172.12.11.90:9080/auth, IsValid: true, Sessions: 0}
 errorHandler: io.realm.SyncManager$1@42068038
 deleteRealmOnLogout: false
 waitForInitialRemoteData: true

正如您可以在" main"上看到创建的领域线程。

PersonFragment代码:

public class PersonsFragment extends Fragment {
    private Realm realm;

    @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.organizations_fragment, container, false);
            realm = Realm.getDefaultInstance();
            return rootView;
        }        

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onDestroy():"
                    + "\nThreadSignature=" + AndroidUtil.getThreadSignature()
                    + "\nRealm global instance=" + Realm.getGlobalInstanceCount(Realm.getDefaultConfiguration())
                    + "\nrealm=" + realm);
        realm.close();
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onDestroy():"
                    + "\nThreadSignature=" + AndroidUtil.getThreadSignature()
                    + "\nrealm.isClosed()=" + realm.isClosed()
                    + "\nRealm global instance=" + Realm.getGlobalInstanceCount(Realm.getDefaultConfiguration())
            );
    }
}

我的片段的logcat:

PersonsFragment(18573):onDestroy():
PersonsFragment(18573):Thread=main:id=1,priority=5,group=main
PersonsFragment(18573):Realm global instance = 1
PersonsFragment(18573):realm = io.realm.Realm@4225bd68  
PersonsFragment(18573):onDestroy():
PersonsFragment(18573):Thread=main:id=1,priority=5,group=main
PersonsFragment(18573):realm.isClosed() = false
PersonsFragment(18573):Realm global instance = 1

正如您所看到的, isClose()仍为 false Realm全局实例始终为= 1,同样Realm实例为在 Main 主题上创建。

1 个答案:

答案 0 :(得分:1)

<强>更新 我刚刚发现了这个问题here

或者你可以按照这个 -

您可以从我issue中学到的东西尝试这一点。您可以打开多个Realm实例,在不同的线程上,您可以尝试使用下面的代码,关闭onDestroy()中的每个Realm实例 -

while(!realm.isClosed()) realm.close();

然后检查isClosed()是否返回true。

另外,为了更好地处理不同线程上的Realm实例,您可以遵循@ Zhuinden的方法 -

try(Realm realm = Realm.getDefaultInstance()) {
   // use realm
} // auto-close

或关闭finally块中的Realm实例。

如果您需要更多帮助,请与我们联系。