Realm not working after migration

风流意气都作罢 提交于 2019-12-11 03:50:00

问题


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
    }
}

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.

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

EDIT: 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.

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
}

回答1:


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 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.

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.



来源:https://stackoverflow.com/questions/32419664/realm-not-working-after-migration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!