迁移后,领域无法正常工作 [英] Realm not working after migration

查看:66
本文介绍了迁移后,领域无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的UIViewController中的相关代码:

This is the relevant code in my UIViewController:

class HabitTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    @IBOutlet weak var habitTableView: UITableView!
    private var _numOfRowsInSects: [Int] = []
    private var _allSections = Set<Int>() //_[0] = 1 -> Morning
    private let _timeInDay = [0: "Morning", 1: "Afternoon", 2:"Evening", 3:"Anytime"]
    private var _habitsBySection:[[Habit]] = []
    private var _whatIsToday = -1 //means no button other than today has been pressed


    override func viewDidLoad() {


        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        habitTableView.delegate = self
        habitTableView.dataSource = self
        var error: NSError?
        NSFileManager.defaultManager().removeItemAtPath(Realm.defaultPath, error:&error)


        let realm = Realm()
        //for testing purposes, preload some habits

        let habit1 = Habit()
        let habit2 = Habit()
        let habit3 = Habit()
        let habit4 = Habit()


        //set up code -- assigning properties and etc.

        realm.write{realm.add(habit1)}
        realm.write{realm.add(habit2)}
        realm.write{realm.add(habit3)}
        realm.write{realm.add(habit4)}

    }


    @IBAction func reloadTableForDay(sender: DayButton){
        if sender.tag != getDayOfWeek(-1){
            _whatIsToday = sender.tag
            _habitsBySection = []
            _allSections = []
           habitTableView.reloadData()
        }
        else{
            _whatIsToday = -1
        }

    }



    func getHabitsForDay(daySelected: Int) ->  Results<Habit> {
        let daySelected = String(daySelected)
        let habitsOfDay = Realm().objects(Habit).filter("durationByDay_days contains %@", "7")

        return habitsOfDay
    }
}

出于测试目的,我设置了要保留在viewDidLoad()中的数据.但是,我的getHabitsForDay(daySelected: Int)函数仅在程序首次运行时返回查询结果,即当我单击调用reloadTableForDay(sender: DayButton)函数的按钮时,依次调用重新加载到UITable时,什么也没有发生,在我的控制台中我可以看到查询返回一个空的Result<Habit>.这一切都是在更改数据模型(添加属性和类)并执行迁移之后发生的.

I set up the data to be persisted in viewDidLoad(), for testing purposes. However my getHabitsForDay(daySelected: Int) function only returns query result when the program first runs, i.e. when I click the buttons that call the reloadTableForDay(sender: DayButton) function, which in turn calls reload to the UITable, nothing happens and in my console I can see the query returned an empty Result<Habit>. This all happened after I changed my data model (added a property and a class) and performed the migration.

我也怀疑 var error: NSError? NSFileManager.defaultManager().removeItemAtPath(Realm.defaultPath, error:&error) 可能会把事情弄糟,但我不确定.

I also suspect that var error: NSError? NSFileManager.defaultManager().removeItemAtPath(Realm.defaultPath, error:&error) could be messing things up, but I'm not sure.

现在,我确定这是由于迁移引起的,因为我启动了一个新项目并复制了代码.一切正常,直到我进行了迁移.

Now i'm sure this was caused by migration, as I started a new project and copied over the code. Everything was working fine until I did a migration.

这是我的AppDelegate中的迁移代码:

This is the migration code in my AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
    let config = Realm.Configuration(
        //You need to increment the version everytime you change your object schema (starts at 0)
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            //If you want to preserve any data, you can do it here, otherwise just leave it blank.
        }
    )

    Realm.Configuration.defaultConfiguration = config

    let realm = Realm()
    return true
}

推荐答案

我同意您删除Realm文件的视图控制器中的代码行很可能是问题的原因.如果要删除默认的Realm文件,在第一次在任何地方调用Realm()之前,这样做会更安全.

I agree that the line of code in your view controller where you're deleting the Realm file would most likely be the cause of the problem. If you want to delete the default Realm file, it would be much safer to do it before Realm() is called anywhere for the first time.

Realm在内存中保留对自身的引用(因此,每次在单独的线程上调用Realm()时,它都不需要不断地设置自身),所以我可以肯定地说,它在内存中的状态可能是与文件打开后被删除混淆.

Realm retains references to itself in memory (So it doesn't need to continually set itself up each time you call Realm() on separate threads), so I'd say it's safe to assume that it's state in memory might be getting confused with the file getting deleted after it was already opened.

如果仅出于测试原因而删除文件,建议您在设置迁移块并首次调用Realm()之前将其删除.

If you're deleting the file, simply for testing reasons, I'd recommend deleting it before you set the migration block and call Realm() for the first time.

这篇关于迁移后,领域无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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