简体   繁体   English

Realm swift:重新启动应用程序后的空领域

[英]Realm swift: Empty realm after relaunching app

I started a new project and tried to implement Realm but I can't get it to work properly.我开始了一个新项目并尝试实施 Realm,但我无法让它正常工作。 My problem is that when I kill my app and relaunch it, all my previously added objects have disappeared and I get empty results from realm.objects.我的问题是,当我杀死我的应用程序并重新启动它时,我以前添加的所有对象都消失了,并且我从realm.objects 得到了空结果。

class RealmManager {
     

static let shared = RealmManager()

let realm: Realm

init() {
    realm = try! Realm()
}

func write(_ completion: ()->Void) {
    do {
        try realm.write() {
            completion()
        }
    } catch {
        print(error)
    }
}

func add(_ object: Object) {
    realm.add(object)
}

func delete(_ object: Object) {
    realm.delete(object)
}

func objects<Element>(_ type: Element.Type) -> Results<Element> where Element : Object {
    return realm.objects(type)
}
}

I created this singleton so I don't have to repeat this realm = try! Realm()我创建了这个单身人士,所以我不必重复这个realm = try! Realm() realm = try! Realm() everywhere in my code. realm = try! Realm()在我的代码中无处不在。 I have this exact same class in another project which works fine.我在另一个工作正常的项目中有这个完全相同的类。

My model looks like this :我的模型是这样的:

class PrepFile: Object {


@objc dynamic var creationDate: Date = Date()
@objc dynamic var lastModificationDate: Date = Date()
@objc dynamic var title: String = "Pas de titre"

@objc dynamic var activityKind: String = ""
@objc dynamic var seanceNumber: Int = 0
@objc dynamic var level: String = ""
@objc dynamic var duration: Int = 0
@objc dynamic var date: Date = Date()
@objc dynamic var cycle: Int = 0

@objc dynamic var mainGoal: String = ""
@objc dynamic var specificGoal: String = ""
@objc dynamic var material: String = ""

@objc dynamic var isDraft: Bool = true

convenience init(title: String? = nil, activityKind: String? = nil, seanceNumber: Int? = nil, level: String? = nil, duration: Int? = nil, date: Date? = nil, cycle: Int? = nil, mainGoal: String? = nil, specificGoal: String? = nil, material: String? = nil, phases: [Phase] = [], isDraft: Bool = true) {
    self.init()
    if let tt = title {
        self.title = tt
    }
    if let ak = activityKind {
        self.activityKind = ak
    }
    if let sn = seanceNumber {
        self.seanceNumber = sn
    }
    if let lv = level {
        self.level = lv
    }
    if let dt = duration {
        self.duration = dt
    }
    if let dt = date {
        self.date = dt
    }
    if let cl = cycle {
        self.cycle = cl
    }
    if let mg = mainGoal {
        self.mainGoal = mg
    }
    if let sg = specificGoal {
        self.specificGoal = sg
    }
    if let mt = material {
        self.material = mt
    }
    self.isDraft = isDraft
}

required init() {
}
}

Then in my VC here's what I do :然后在我的 VC 这就是我所做的:

class PrepFileListViewController: UIViewController {


@IBOutlet weak var tableView: UITableView!

lazy var prepFiles: Results<PrepFile> = { RealmManager.shared.objects(PrepFile.self) }()
var completePrepFiles: [PrepFile] = []
var draftPrepFiles: [PrepFile] = []

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    
    RealmManager.shared.write {
        for file in prepFiles {
            RealmManager.shared.delete(file)
        }
    }
    RealmManager.shared.write() {
        RealmManager.shared.add(PrepFile(title: "Fiche de prep 1"))
        RealmManager.shared.add(PrepFile(title: "Fiche de prep 2"))
        RealmManager.shared.add(PrepFile(title: "Fiche de prep 3"))
        RealmManager.shared.add(PrepFile(title: "Fiche de prep 4"))
        RealmManager.shared.add(PrepFile(title: "Fiche de prep 5"))
    }
}

override func viewWillAppear(_ animated: Bool) {
    prepFiles = RealmManager.shared.objects(PrepFile.self)
    completePrepFiles = prepFiles.filter({ !$0.isDraft })
    draftPrepFiles = prepFiles.filter({ $0.isDraft })
    tableView.reloadData()
}
}

Now when I run this, it works fine.现在当我运行它时,它工作正常。 My PrepFile are added to Realm and I retrieve them alright with my RealmManager.shared.objects(PrepFile.self).我的 PrepFile 被添加到 Realm,我用我的 RealmManager.shared.objects(PrepFile.self) 检索它们。 Now, I comment the part where I delete/add my files in the viewDidLoad and I get nothing.现在,我评论了在 viewDidLoad 中删除/添加文件的部分,但我什么也没得到。 I don't get empty objects from RealmManager.shared.objects(PrepFile.self), I get an empty result like nothing was ever saved there.我没有从 RealmManager.shared.objects(PrepFile.self) 得到空对象,我得到一个空的结果,就像那里没有保存任何东西一样。

What am I doing wrong ?我究竟做错了什么 ?

I am using Xcode 12 and running my app on an iPhone 11 / 13.3 simulator.我正在使用 Xcode 12 并在 iPhone 11 / 13.3 模拟器上运行我的应用程序。 Realm version is 5.5.0. Realm 版本是 5.5.0。

Hmm from what I can guess, you either have somewhere else a configuration for realm to be "inMemoryIdentifier" or you do not set the proper configuration with fileURL at beggining of the app.嗯,据我所知,您要么在其他地方将领域配置为“inMemoryIdentifier”,要么在应用程序开始时没有使用 fileURL 设置正确的配置。 Configuration local realm: https://realm.io/docs/swift/latest/#realms配置本地域: https : //realm.io/docs/swift/latest/#realms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM