2016-07-18 128 views
1

我使用Realm與我的android項目。目前,我定義默認領域配置在我的應用程序類如下 -領域配置管理

@Override 
public void onCreate(){ 
    super.onCreate(); 
    myApplication=this; 
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).deleteRealmIfMigrationNeeded().build(); 
    Realm.setDefaultConfiguration(realmConfiguration); 
    Thread.setDefaultUncaughtExceptionHandler(new UnhandledExceptionHandler()); 

} 

在當前情況下只有1個用戶使用設備。她使用她的憑據登錄到應用程序並使用默認領域配置。

但是,有了新的要求,同一個android設備可以被不同的用戶使用,因此我需要每個用戶有不同的領域配置,這樣每個用戶都有自己的領域文件嗎?

如果這是真的,那麼管理領域配置的最好方法是什麼?我應該在登錄活動中這樣做嗎?我應該在登錄活動中爲每個用戶創建不同的領域配置嗎?

感謝

Apurva

+0

爲了打開領域,您需要**相同的配置實例** **每個** Real **實例,您打開該給定的領域。記住這一點。讓它成爲單身。可能與枚舉實例或匕首。 – EpicPandaForce

+0

切換用戶時應該如何使用singleton?我正在考慮使用此模式(來自realm.io網站) - RealmConfiguration myConfig = new RealmConfiguration.Builder(context) .name(「myrealm.realm」) .schemaVersion(2) .modules(new MyCustomSchema()) .build(); RealmConfiguration otherConfig =新RealmConfiguration.Builder(上下文) 。名稱( 「otherrealm.realm」) .schemaVersion(5) .modules(新MyOtherSchema()) .build(); Realm myRealm = Realm.getInstance(myConfig); Realm otherRealm = Realm.getInstance(otherConfig); – user6392598

+0

如何檢查領域配置是否存在? – user6392598

回答

1

IMO,使用工廠類將是有益的,因爲你管理多個領域實例。它可能看起來像這樣,

public class RealmFactory { 
/* Realm 
* CAUTION: Be careful which thread you call this from, it is not Thread safe */ 
public static Realm getRealmInstance(Context context, String primaryKeyFromUser) { 
    return Realm.getInstance(getRealmConfiguration(context, primaryKeyFromUser)); 
} 

/* RealmConfiguration */ 
private static RealmConfiguration getRealmConfiguration(Context context, String primaryKeyFromUser) { 
    return new RealmConfiguration.Builder(context) 
      .name(primaryKeyFromUser) 
      .encryptionKey(getSecurityKey()) 
      .deleteRealmIfMigrationNeeded() 
      .build(); 
} 

/* SecurityKey, 
* CAUTION: This is just a sample */ 
private static byte[] getSecurityKey() { 
    char[] chars = "16CharacterLongPasswordKey4Realm".toCharArray(); 
    byte[] key = new byte[chars.length * 2]; 
    for (int i = 0; i < chars.length; i++) { 
     key[i * 2] = (byte) (chars[i] >> 8); 
     key[i * 2 + 1] = (byte) chars[i]; 
    } 

    return key; 
} 

/* Check for Realm file */ 
public static boolean isRealmFileExists(Context context, String primaryKeyFromUser) { 
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context) 
      .name(primaryKeyFromUser) 
      .encryptionKey(getSecurityKey()) 
      .deleteRealmIfMigrationNeeded() 
      .build(); 

    File realmFile = new File(realmConfiguration.getPath()); 
    return realmFile.exists(); 
} 

/* Delete Realm file, if exists 
* CAUTION: if the Realm instance with given configuration is open, make sure you close it 
*   first, before deleting it, else an Exception will be thrown. */ 
public static void deleteRealmFile(Context context, String primaryKeyFromUser) { 
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context) 
      .name(primaryKeyFromUser) 
      .build(); 
    Realm.deleteRealm(realmConfiguration); 
} 

/* Delete all Realm files, if exists 
* CAUTION: if the Realm instance with given configuration is open, make sure you close it 
*   first, before deleting it, else an Exception will be thrown. */ 
public static void deleteAllRealmFiles(Context context) { 
    File file = new File(context.getFilesDir().getPath()); 

    File list[] = file.listFiles(new FileFilter() { 
     @Override 
     public boolean accept(File pathname) { 
      return pathname.isFile(); 
     } 
    }); 

    for (File deleteFile : list) { 
     RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context) 
       .name(deleteFile.getName()) 
       .build(); 
     Realm.deleteRealm(realmConfiguration); 
    } 
} 
} 
+0

非常感謝Viraj!你的榜樣幫了我很多! – user6392598