Realm-cocoa: how to create multi realms in one app

Created on 21 Sep 2016  路  6Comments  路  Source: realm/realm-cocoa

I have multi databases in one app, and it seems all of the tables will be included in every realm db but the data will be saved only in the target realm db, and here is my base code for every db :

-(void)initDBWithName:(NSString *)name
               UserID:(NSString *)uid
              version:(int)version {

    RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
    NSString *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    NSString* path =[NSString stringWithFormat:@"%@/DB_Realm/%@-%@.%@",
                    libraryDirectory,
                    name,
                    uid,
                    kTOONRealmDBExtension];
    NSURL* ru = [NSURL URLWithString:path];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[path stringByDeletingLastPathComponent] ])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:[path stringByDeletingLastPathComponent]
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:nil];
    }
    configuration.fileURL = ru;
    NSLog(@"------------realm path: %@",path );
    configuration.schemaVersion = kTNRLMDB_Version;

self.currentConfiguration = configuration;

  NSError *rlmError;
    RLMRealm *cr = [RLMRealm realmWithConfiguration:self.currentConfiguration
                                                   error:&rlmError];


}
T-Help

Most helpful comment

Apologies, I don't understand what you're asking? If you want to use multiple databases, make a file URL for each database you want, then create RLMRealm instances using realmWithConfiguration:error and a configuration with that file URL set.

All 6 comments

Apologies, I don't understand what you're asking? If you want to use multiple databases, make a file URL for each database you want, then create RLMRealm instances using realmWithConfiguration:error and a configuration with that file URL set.

for example, I have two databases: db1 & db2, table1 and table 2 are included in db1; table3 and table 4 are included in db2, but when I browse the realm file of db1 and db2, both of them have all of the tables of table1, table2, table3, table4...

@pkjijie you need to specify objectClasses property of your RLMRealmConfiguration for each realm, see more in Docs: https://realm.io/docs/objc/latest/#class-subsets

Closing since @stel's last comment seems like the right suggestion to me, and we haven't heard back from @pkjijie in over a week.

There is one thing I don't understand. I have 2 frameworks working with realm. Each one of them has different configuration and completely different URL paths and tables. To my surprise I go to one of the realm db files and it shows me the tables from the other framework? Why? They are completely separate pieces of code

Facing the same issue as @trant. Even though i have two separate configurations, the tables are appearing in both the realm files. Any workaround for this ?

Was this page helpful?
0 / 5 - 0 ratings