我是新來的領域,所以當我與它混在一起學習它時,我發現了一些非常有趣的東西。在我的appDelegate:領域設置自定義fileURL混淆
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.Hobo.RealmDatabase")!
var config = Realm.Configuration()
config.fileURL = directory.filePathURL?.URLByAppendingPathComponent("db.realm")
Realm.Configuration.defaultConfiguration = config
let realm = try! Realm()
print("File Location: \(realm.configuration.fileURL!)") // -> Location A
print("NO OF USERS: \(realm.objects(User).count)") // -> 0
return true
}
,但在我的ViewController:
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
print("NO OF USERS IN VIEWDIDLOAD: \(realm.objects(User).count)") // -> 1
let firstTime = loadFirstTime()
if firstTime {
// configure USER!
let user = User()
user.monthlyIncome = 50000
try! realm.write({
realm.add(user)
})
saveFirstTime(false)
print("First time, user written")
}
dailyLimit.text = String(realm.objects(User).first!.dailyLimit)
}
通知從打印的回報()函數。在應用程序委託中,打印結果(用戶數:)會返回0,但在viewController的viewDidLoad中,它返回1.
是不是都應該返回相同的值?在這種情況下1?
在此先感謝!
嗨,感謝您的建議。我發現ViewController的Realm的fileURL是default.realm,而不是分配的App Group。爲什麼?再次感謝!! –
@RaymondMoay我編輯了答案 – MCMatan
謝謝!解決了。 (: –