Ios 域在迁移后不工作
这是myIos 域在迁移后不工作,ios,realm,Ios,Realm,这是myUIViewController中的相关代码: class HabitTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ @IBOutlet weak var habitTableView: UITableView! private var _numOfRowsInSects: [Int] = [] private var _allSections
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
}
}
我同意视图控制器中删除领域文件的代码行很可能是问题的原因。如果您想删除默认的领域文件,在第一次调用
Realm()
之前删除它会更安全
Realm在内存中保留对自身的引用(因此它不需要每次在单独的线程上调用Realm()
),所以我可以安全地假设它在内存中的状态可能会与文件在打开后被删除混淆
如果您只是出于测试原因而删除文件,我建议您在设置迁移块并第一次调用
Realm()
之前删除它。我取出了迁移代码,一切都恢复正常。。。。现在抓我的头。。。。
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
}