使用Xcode-8.2.1,Swift-3.0.2,RealmSwift-2.2.0,iOS-Simulator-10:
试图用Realm-Object编写一个View-Model,我在另一个线程中创建一个returnArray失败了。 问题是对先前创建的realm-object的访问失败(很可能是由于后台线程访问??)。
任何人都可以告诉我下面的代码有什么问题(见下文):重要:在“getEntries-completionHandler”之前调用“createDataEntries() - method”(可以看到正确的SimPholders realmobject-entry) )! 因此“类别”设置为“爱”(参见代码)
import Foundation import RealmSwift class MVVMCBalancesModel: BalancesModel { fileprivate var entries = [BalancesDataEntry]() let realm = try! Realm() init() { self.createDataEntries() } fileprivate func createDataEntries() { let myBalance = BalancesDataEntry() myBalance.index = 0 myBalance.category = "Love" // !!!!!!! Here the category is filled try! self.realm.write { self.realm.deleteAll() self.realm.add(myBalance) } } func getEntries(_ completionHandler: @escaping (_ entries: [BalancesDataEntry]) -> Void) { // Simulate Aysnchronous data access DispatchQueue.global().async { var returnArray: [BalancesDataEntry] = [BalancesDataEntry]() let realmy = try! Realm() let cnt = realmy.objects(BalancesDataEntry.self).count for idx in 0 ..< cnt { let obj = realmy.objects(BalancesDataEntry.self).filter("index = \(idx)").first! returnArray.append(obj) } completionHandler(returnArray) // !!!!!!! BREAKPOINT (see screenshot below) } } }运行上面的代码并在completionHandler(returnArray)上设置断点会产生以下结果:
为什么returnArray的“类别”是一个空字符串?
Using Xcode-8.2.1, Swift-3.0.2, RealmSwift-2.2.0, iOS-Simulator-10:
Trying to write a View-Model with a Realm-Object, I fail at creating a returnArray in another thread. The issue is that the access to the previously created realm-object fails (most likely due to the background-thread access ??).
Can anybody tell me what is wrong with the following code (see below): Important: It is given that the "createDataEntries()-method" is called before the "getEntries-completionHandler" (as can be seen with correct SimPholders realmobject-entry)! Therefore the "category" is set as "Love" (see code)
import Foundation import RealmSwift class MVVMCBalancesModel: BalancesModel { fileprivate var entries = [BalancesDataEntry]() let realm = try! Realm() init() { self.createDataEntries() } fileprivate func createDataEntries() { let myBalance = BalancesDataEntry() myBalance.index = 0 myBalance.category = "Love" // !!!!!!! Here the category is filled try! self.realm.write { self.realm.deleteAll() self.realm.add(myBalance) } } func getEntries(_ completionHandler: @escaping (_ entries: [BalancesDataEntry]) -> Void) { // Simulate Aysnchronous data access DispatchQueue.global().async { var returnArray: [BalancesDataEntry] = [BalancesDataEntry]() let realmy = try! Realm() let cnt = realmy.objects(BalancesDataEntry.self).count for idx in 0 ..< cnt { let obj = realmy.objects(BalancesDataEntry.self).filter("index = \(idx)").first! returnArray.append(obj) } completionHandler(returnArray) // !!!!!!! BREAKPOINT (see screenshot below) } } }Running the above code and setting a breakpoint at the completionHandler(returnArray) produces the following:
Why is the "category" of the returnArray an empty String ???
最满意答案
从Realm中检索的对象的属性是从底层存储中延迟检索的。 从Swift访问属性将返回适当的值。 同样,如果从Xcode的LLDB控制台运行po returnArray ,您应该看到对象的完整状态。 调试器弹出窗口中显示的实例变量仅在对象未受管理时(在添加到Realm之前)使用。
Properties of objects retrieved from a Realm are lazily retrieved from the underlying storage. Accessing the properties from Swift will return the appropriate values. Likewise, if you run po returnArray from Xcode's LLDB console you should see the object's complete state. The instance variables, shown in the debugger popover, are only used when the object is unmanaged (prior to being added to the Realm).
更多推荐
发布评论