gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:18:47 25 4
gpt4 key购买 nike

我有一个应用程序,用户可以保存他们的心电图。我想按用户拆分 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com