Clear complete Realm Database

后端 未结 6 2046
半阙折子戏
半阙折子戏 2020-12-24 11:45

I\'m playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user de

6条回答
  •  半阙折子戏
    2020-12-24 12:14

    I ran into this fun little issue. So instead i queried the schema version directly before running the schemamigration.

    NSError *error = NULL;
    NSUInteger currentSchemaVersion = [RLMRealm schemaVersionAtPath:[RLMRealm defaultRealmPath] error:&error];
    if (currentSchemaVersion == RLMNotVersioned) {
        // new db, skip
    
    } else if (currentSchemaVersion < 26) {
        // kill local db
        [[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:&error];
        if (error) {
            MRLogError(error);
        }
    
    } else if (error) {
        // for good measure...
        MRLogError(error);
    }
    
    // perform realm migration
    [RLMRealm setSchemaVersion:26
                forRealmAtPath:[RLMRealm defaultRealmPath]
            withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
    
            }];
    

提交回复
热议问题