Realm Async Thread Swift 3(Realm Async Thread Swift 3)

编程入门 行业动态 更新时间:2024-10-23 23:23:06
Realm Async Thread Swift 3(Realm Async Thread Swift 3)

使用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).

更多推荐

本文发布于:2023-08-02 12:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Async   Realm   Swift   Thread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!