ios - 在 iOS 上打开多个 Realm 文件

标签 ios objective-c xcode cocoa realm

我有一个应用程序,用户可以保存他们的心电图。我想按用户拆分 Realm 使用情况。我正在开发一个 user.realm(引用为 _realmUsers)来保存所有登录应用程序的用户,以及一个 userID.realm(被引用为 _realm 并且他们将是每个登录应用程序的用户一次)用户保存他的数据的地方。如何实现?因为如果我写这样的东西

两个 Realm 的初始化

+ (void)initializeRealmForUsers
{
    // Open the encrypted Realm file
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
                       URLByAppendingPathComponent:USER_REALM]
                      URLByAppendingPathExtension:@"realm"];

    NSError *err;
    RLMRealm *realmUser = [RLMRealm realmWithConfiguration:config error:&err];
    if(err == nil && realmUser != nil)
        [sharedInstance setRealmUsers:realmUser];
}

+ (void)initializeDataRealmWithUserID:(NSString*)userUUID
{
    // Open the encrypted Realm file
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.fileURL = [[[config.fileURL URLByDeletingLastPathComponent]
                       URLByAppendingPathComponent:userUUID]
                      URLByAppendingPathExtension:@"realm"];

    NSError *err;
    RLMRealm *realmUser = [RLMRealm realmWithConfiguration:config error:&err];
    if(err == nil && realmUser != nil)
        [sharedInstance setRealmUsers:realmUser];

    [[RealmManager sharedInstance] deleteIncompleteExam];

}

创建用户:

- (User *)createUserWithData:(NSDictionary *)data
{
    [_realmUsers beginWriteTransaction];

    User *newUser = [User new];
    [newUser setEmail:[data objectForKey:USER_EMAIL]];
    [newUser setPassword:[CryptoUtils MD5String:[data 
    objectForKey:USER_PASSWORD]]];
    [newUser setLogged:@NO];

    [_realmUsers addOrUpdateObject:newUser];
    [_realmUsers commitWriteTransaction];

    return newUser;
}

Patient的创建,第二 Realm (特定用户 Realm )的记录类型:

- (Patient *)createPatientWithData:(NSDictionary *)data forUser:(BOOL)forUser
{
    Patient *newPatient;
    if([[RealmManager sharedInstance] getSelfPatient] == nil)
        newPatient = [Patient new];
    else
        newPatient = [[RealmManager sharedInstance] getSelfPatient];

    [_realm beginWriteTransaction];
    [newPatient setFirstName:[data objectForKey:PATIENT_FIRSTNAME]];
    [newPatient setLastName:[data objectForKey:PATIENT_LASTNAME]];
    [newPatient setSelfPatient:forUser];
    [_realm addOrUpdateObject:newPatient];
    [_realm commitWriteTransaction];

    return newPatient;
}

和登录:

- (User *)loginWithUsername:(NSString *)email password:(NSString *)password andData:(NSDictionary *)userData
{
    User *user = [self userWithUsername:email];
    if(user == nil){
        NSMutableDictionary *mutableData = [NSMutableDictionary new];
        [mutableData addEntriesFromDictionary:userData];
        [mutableData setObject:password forKey:USER_PASSWORD];
        user = [self createUserWithData:mutableData];
    } else{
        [_realmUsers beginWriteTransaction];
        [user setPassword:[CryptoUtils MD5String:password]];
        [_realmUsers commitWriteTransaction];
    }

    [_realmUsers beginWriteTransaction];
    [user setLogged:@YES];
    [_realmUsers commitWriteTransaction];

    [RealmManager initializeDataRealmWithUserID:[user uuid]];
    [self createPatientWithData:@{PATIENT_FIRSTNAME : [userData objectForKey:PATIENT_FIRSTNAME], PATIENT_LASTNAME : [userData objectForKey:PATIENT_LASTNAME]} forUser:YES];

    return user;
}

登录后,我的调用如 [User allObjects](用户必须在 user.realm 上)[Patient allObject](患者必须在 userID.realm 上)给出我是空结果。

最佳答案

您可以通过以下方式访问来自不同 Realm 的数据

[User allObjectsInRealm: user.realm]

[User objectsInRealm:user.realm where:@""];

关于ios - 在 iOS 上打开多个 Realm 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44584645/

相关文章:

objective-c - 自动布局不适用于多行 UILabel

iphone - 如何将 nsnumber 转换为 float

swift - 是否可以在 Xcode 中运行单个 Swift 文件 main()?

ios - UITapGestureRecognizer 干扰 UISlider

ios - 将自定义 View 锚定到 Swift 中 Mapbox NavigationViewController 中的现有 View

ios - getControlPointAtIndex 参数类型

ios - 压缩 Xcode 项目

ios - 如何在 SpriteKit 中为关节添加纹理?

iphone - 如何在 iOS 中更改 ePub 图书的字体颜色和字体样式?

iPhone ui 导航 - 从页面跳转