Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring a Realm file to be deleted when a migration is required #1692

Closed
pronebird opened this issue Mar 26, 2015 · 14 comments · Fixed by #3463
Closed

Allow configuring a Realm file to be deleted when a migration is required #1692

pronebird opened this issue Mar 26, 2015 · 14 comments · Fixed by #3463
Milestone

Comments

@pronebird
Copy link

I have a cache storage which is ok to be wiped on schema changes. I don't want to migrate it since it's transitory anyway. Is there any way to nuke my realm when schema inconsistency found? This would also help a lot during development when schema changes occur pretty often.

I found a workaround but that requires a version bump, which I can probably simulate by using bundle version. What would be the official way? :)

#import <Realm/RLMObjectSchema.h>

[RLMRealm setSchemaVersion:1 forRealmAtPath:realmPath withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
    for(RLMObjectSchema *objectSchema in migration.oldSchema.objectSchema) {
        [migration enumerateObjects:objectSchema.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            [migration deleteObject:newObject];
        }];
    }
}];

RLMRealm  *cleanAndTidy = [RLMRealm realmWithPath:realmPath];

The better option I found is to nuke realm when things go south:

#import <Realm/RLMConstants.h>

static void RLMCleanupFiles(NSString *path) {
    // delete files starting with xxx.realm* name
}

@try {
    cacheRealm = [RLMRealm realmWithPath:realmPath];
}
@catch(NSException *exception) {
    NSLog(@"Caught exception while opening Realm: %@", exception);

    if([exception.name isEqualToString:RLMExceptionName]) {
        RLMCleanupFiles(realmPath);
        cacheRealm = [RLMRealm realmWithPath:realmPath];
    }
    else {
        @throw; // rethrow
    }
}
@segiddins
Copy link
Contributor

@pronebird we're going to be introducing the concept of an RLMConfiguration that will likely give you the ability to specify a "delete if migration needed" option. Until then, I can't really think of a great way to do this in Swift.

@alazier
Copy link
Contributor

alazier commented Mar 27, 2015

I'm not sure I like the idea of specifying this in RLMConfiguration. I think in this case it might be nice to add a new method -clearRealm to the RLMMigration object.

@jpsim
Copy link
Contributor

jpsim commented Mar 27, 2015

I think in this case it might be nice to add a new method -clearRealm to the RLMMigration object.

That would require increasing the schema version and defining a migration block, which I don't think would serve the needs prompting users to ask for this.

As much as I'd like to discourage the behavior of deleting realms on schema mismatch, the truth is that users are already finding hacky and dangerous ways to do this (try/catch block around first RLMRealm access). Maybe we should instead publicly expose something like +[RLMRealm requiresMigrationAtPath:encryptionKey:error:].

@lordkev
Copy link

lordkev commented Mar 31, 2015

Just to chime in, as we're running into this too. Currently our use case is for alpha/beta versions of an app in development. We'd prefer not to bother with migrations every time we change the schema in a non-production app.

Additionally, our use of Realm is as a persistent local cache that can be re-created if necessary. I understand discouraging the behavior of deleting realms on schema mismatch, but it's also definitely useful to have deleting the realm as an option of last resort if a migration goes south for some reason. We do a similar thing with Core Data in another app, and although it's considered the "nuclear" option, it's better than crashing at every startup.

@jpsim
Copy link
Contributor

jpsim commented Apr 2, 2015

#1723 is now pending review, which should enable safely deleting a realm file on schema mismatch.

@jpsim jpsim removed their assignment May 12, 2015
@jpsim jpsim added P1 and removed pr labels May 12, 2015
@jpsim jpsim changed the title Can I wipe my realm on migration instead of crash? Allow configuring a Realm file to be deleted when a migration is required May 12, 2015
@ikonst
Copy link

ikonst commented Jul 27, 2015

@jpsim #1723 was closed without merging. Now all hope is on #1584. Is it coming up?

@jpsim
Copy link
Contributor

jpsim commented Jul 27, 2015

Now all hope is on #1584. Is it coming up?

That's still our preferred direction, but no one on our team is actively working on it at the moment. However, since this functionality can be built entirely at the Objective-C level, it's actually a task anyone from the community could contribute to Realm, if you're looking to get involved.

We're currently quite involved with fine-grain notifications, KVO, cascading deletes, thread handover and preparing an official release with functionality that recently landed in master.

@ikonst
Copy link

ikonst commented Jul 27, 2015

I just might.

@segiddins
Copy link
Contributor

This will be trivial to implement via RLMRealmConfiguration once #2252 is merged

@bdash bdash modified the milestones: 0.101, 0.100 Apr 29, 2016
@jpsim jpsim removed the S:Review label May 9, 2016
@shfishburn
Copy link

How do we implement this in Swift 2.0?

@bdash
Copy link
Contributor

bdash commented Jul 29, 2016

@shfishburn
Copy link

Read that before. If I may ask, where do we inject that line?

From: Mark Rowe notifications@github.com notifications@github.com
Reply: realm/realm-cocoa
reply@reply.github.com
reply@reply.github.com
Date: July 29, 2016 at 14:08:19
To: realm/realm-cocoa realm-cocoa@noreply.github.com
realm-cocoa@noreply.github.com
CC: Stephen Fishburn stephen@getwellcities.com stephen@getwellcities.com,
Comment comment@noreply.github.com comment@noreply.github.com
Subject: Re: [realm/realm-cocoa] Allow configuring a Realm file to be
deleted when a migration is required (#1692)

Using Realm.Configuration.deleteRealmIfMigrationNeeded

https://realm.io/docs/swift/latest/api/Classes/Realm/Configuration.html#/s:vVC10RealmSwift5Realm13Configuration28deleteRealmIfMigrationNeededSb
.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#1692 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHCt05o8f1e3eOBeqz9_Gf60wT7IN4CGks5qamvDgaJpZM4D1Jeb
.

@jpsim
Copy link
Contributor

jpsim commented Aug 2, 2016

If I may ask, where do we inject that line?

@shfishburn you pass a Realm.Configuration value when initializing a Realm: Realm.init(configuration:). So wherever you're opening a Realm for the first time in your app, you should make sure that the configuration value used has the deleteRealmIfMigrationNeeded value set to what you want it.

@NikKovIos
Copy link

NikKovIos commented Feb 17, 2017

Here is how to use it. Thumbs up if did help.

didFinishLaunchingWithOptions {
     let configuration = Realm.Configuration(deleteRealmIfMigrationNeeded: true)
     Realm.Configuration.defaultConfiguration = configuration
     do { _ = try Realm() } catch {}
}
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    config.deleteRealmIfMigrationNeeded = YES;
    config.schemaVersion = 2; 
    [RLMRealmConfiguration setDefaultConfiguration:config];
    [RLMRealm defaultRealm];
    NSLog(@">>> Realm path:  %@", [config fileURL]);

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.