为 Realm Cloud 中的所有领域重置架构 [英] Reset schema for all Realms in Realm Cloud

查看:69
本文介绍了为 Realm Cloud 中的所有领域重置架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以完全消除 Realm Cloud 中的所有内容,包括现有的架构定义?

Is there any way I can completely nuke everything from my Realm Cloud, including existing schema definitions?

推荐答案

有一种方法可以从 Realm Object Server 中删除 Realms.

There is a way to delete the Realms from the Realm Object Server.

以下是我在 领域收集的信息论坛帖子

这是官方文档.

这非常重要.我链接的文档适用于 Docs 3.0.自托管似乎即将消失,因此 3.16 文档不再包含此信息.

This is super important though. The docs I am linking are for Docs 3.0. Self-hosting appears to be going away so the 3.16 Docs no longer include this information.

有两个步骤

Remove server files
Remove all local files

这两项都必须完成,否则 Realm 会尝试自行重新同步,您的数据将永远不会消失.

These both have to be done or else Realm will try to re-sync itself and your data will never go away.

第一个函数删除 Realm Cloud 实例,如果成功,则删除本地 Realm 文件.

The first function deletes a Realm Cloud instance and if successful, deletes the local realm files.

//
//MARK: - delete database
//
func handleDeleteEverything() {
    let realm = RealmService //Singleton that returns my realm cloud
    try! realm.write {
        realm.deleteAll()
    }

    guard let currentUser = SyncUser.current else {return}
    let adminToken = currentUser.refreshToken!

    let urlString = "https://your_realm.cloud.realm.io" //from RealmStudio upper right corner
    let endPoint = "\(urlString)/realms/files/realm_to_delete"
    let url = URL(string: endPoint)
    var request = URLRequest(url: url!)
    request.httpMethod = "DELETE"
    request.addValue(adminToken, forHTTPHeaderField: "Authorization")

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        if let err = error {
            print("err = \(err.localizedDescription)")
            return
        }

        print("Realm has been deleted")
        self.deleteLocalRealmFiles() //remove local files
    }
    task.resume()
}

然后是删除本地文件的函数.此功能与 Realm 论坛帖子中显示的有所不同,但在 Realm 4.2 中添加了此功能

and then the function to remove the local files. This function is a bit different than what appears on the Realm Forums post with the addition of this function in Realm 4.2

try Realm.deleteFiles(for: config)

以及调用它的函数

func deleteLocalRealmFiles() {

    do {
        let config = Realm.Configuration.defaultConfiguration
        let isSuccess = try Realm.deleteFiles(for: config)
        if isSuccess == true {
            print("local files were located and deleted")
        } else {
            print("no local files were deleted, files were not found")
        }

    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

这篇关于为 Realm Cloud 中的所有领域重置架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆