element-hq / element-hq/element-ios

Be more robust when opening realm DBs

Open
#2,936 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
1.8k
Forks
544
PR merge metrics
PR metrics pending

Description

We should reset DB when it fails to open to avoid the app crashes in loop.

Proposed solution before I lost the code:
```
- (nullable RLMRealm*)realm
{
NSError *error;

RLMRealmConfiguration *realmConfiguration = self.realmConfiguration;
RLMRealm *realm = [RLMRealm realmWithConfiguration:self.realmConfiguration error:&error];

if (error)
{
NSLog(@"[MXRealmAggregationsStore] realmForUser gets error: %@", error);

NSLog(@"[MXRealmAggregationsStore] realmForUser: Retrying...");
[NSFileManager.defaultManager removeItemAtURL:realmConfiguration.fileURL error:nil];
realmConfiguration = self.realmConfiguration;
realm = [RLMRealm realmWithConfiguration:self.realmConfiguration error:&error];
if (realm)
{
NSLog(@"[MXRealmAggregationsStore] realmForUser: Realm db fixed");
}
else
{
NSLog(@"[MXRealmAggregationsStore] realmForUser: Still failing: %@", error);
}
}

return realm;
}
```

Contributor guide

Open the contributing guide

Research direction

Start by locating the realm method that uses RLMRealmConfiguration and RLMRealm realmWithConfiguration:error:. Reproduce or trace the failed database-opening path, then verify that a failed open is handled by removing the database and retrying, with a successful retry returning a realm instead of leaving the app in a crash loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
objective-c
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.