【问题标题】:Multiple Realm File opened on iOS在 iOS 上打开多个领域文件
【发布时间】:2017-06-16 08:39:06
【问题描述】:

当用户可以保存他们的心电图时,我有一个应用程序。我想按用户拆分 Realm 使用情况。我正在开发一个user.realm(引用为_realmUsers)来保存所有登录应用程序的用户,以及一个userID.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 *)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;
}

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

【问题讨论】:

    标签: ios objective-c xcode cocoa realm


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。这就是解决方案。
    • 快乐编码:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多