如何确保变量中的数据在使用之前已加载 [英] How to make sure data in variable is loaded before using it

查看:26
本文介绍了如何确保变量中的数据在使用之前已加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 .onRecieve 方法

.onReceive(model2.$postsById) { postById in
       print("MODEL2 \(self.model2.postsById)")
    }

打印出来的东西是这样的

Which prints out something like this

MODEL2 [Test.PostById(id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: nil, path: "public/1584106997200-astronomy-space-abstract-galaxy.jpg"), Test.PostById(id: "7ac7ff5f-4b4b-458f-b9a6-491813dbe97d", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("California Wind "), path: "public/1584106997230-California_Wind.mp3"), Test.PostById(id: "d804c511-fdd6-453c-816d-fbe501b0ba8d", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("Cosmic Love "), path: "public/1584106997710-Cosmic_Love.mp3"), Test.PostById(id: "094e5b65-f0b2-4a5d-8c20-c6fa3d733325", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("Happy Sixth "), path: "public/1584106997890-Happy_Sixth.mp3"), Test.PostById(id: "f148fd8e-6896-492b-8e5a-c99072cad562", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("How it Began "), path: "public/1584106998000-How_it_Began.mp3")]
MODEL2 [Test.PostById(id: "922f6ffc-8738-4bc2-a96d-654678540f5a", album_id: "922f6ffc-8738-4bc2-a96d-654678540f5a", name: nil, path: "public/1584045867007-71188186_249463072641040_8764351972645011456_n.jpg"), Test.PostById(id: "ffb572f9-bc40-412f-9514-e413da06da8e", album_id: "922f6ffc-8738-4bc2-a96d-654678540f5a", name: Optional("Shameless Life Wisdom Sendoff "), path: "public/1584045867009-Shameless_Life_Wisdom_Sendoff.mp3"), Test.PostById(id: "98b9bbfa-346e-4571-81a2-38afd8c5353c", album_id: "922f6ffc-8738-4bc2-a96d-654678540f5a", name: Optional("Simmering "), path: "public/1584045867657-Simmering.mp3"), Test.PostById(id: "65ccf88b-5754-4981-ab06-cf4cdc93eca8", album_id: "922f6ffc-8738-4bc2-a96d-654678540f5a", name: Optional("Skewls Owt "), path: "public/1584045867892-Skewls_Owt.mp3"), Test.PostById(id: "6b350c4d-c6b1-4679-b7d5-2c3bd33459f5", album_id: "922f6ffc-8738-4bc2-a96d-654678540f5a", name: Optional("Still Not Rite "), path: "public/1584045868127-Still_Not_Rite.mp3")]
MODEL2 [Test.PostById(id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: nil, path: "public/1584038265741-Profile-img-icon.png"), Test.PostById(id: "4e9d5c31-d48e-4d0a-8f26-ba67d1ac39c2", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Prophet 7 "), path: "public/1584038265742-Prophet_7.mp3"), Test.PostById(id: "71b80407-2434-445e-8e0b-30e1a45293a7", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Rubber Ducky "), path: "public/1584038265907-Rubber_Ducky.mp3"), Test.PostById(id: "0069ba6a-85c2-48d8-b5bd-d0e70c84bea2", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Shameless Life Wisdom Sendoff "), path: "public/1584038265996-Shameless_Life_Wisdom_Sendoff.mp3")]
MODEL2 [Test.PostById(id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: nil, path: "public/1584038190074-audioplayer.jpg"), Test.PostById(id: "9201b562-d5d9-4e6f-8b21-4c3f691b62ee", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("Happy Sixth "), path: "public/1584038190075-Happy_Sixth.mp3"), Test.PostById(id: "f3ca3cb8-7670-4e0b-87b3-80f8a2d3d96f", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("How it Began "), path: "public/1584038190273-How_it_Began.mp3"), Test.PostById(id: "63c58525-3115-4882-b280-66c675c64ded", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("It Doesnt Need To Be More Than That "), path: "public/1584038190375-It_Doesnt_Need_To_Be_More_Than_That.mp3"), Test.PostById(id: "af5c2f62-fa97-4736-a003-cd876324d28c", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("Kindergarden "), path: "public/1584038190484-Kindergarden.mp3")]

...还有更多

这似乎是我所有的数据

我将没有 .onRecieve 的 model2.postsById 传递到我的 struct Album 中:

I'm passing model2.postsById without an .onRecieve into my struct Album:

struct ContentView: View {
  @ObservedObject var model = PostListViewModel()
  @ObservedObject var model2 = PostListViewByIdModel()

        var body: some View {

            NavigationView {
                List(model.posts) { post in
                 VStack{
                        Text("Title: ").bold()
                            + Text("\(post.title)")
                    NavigationLink(destination: Album(post: post, post2: self.model2.postsById)) {

在线:NavigationLink(destination: Album(post: post, post2: self.model2.postsById))

我想在我的列表中使用的内容

Which I'm trying to use in my List

struct Album: View {
    var post:Post
    var post2:[PostById]

    var body: some View {
             VStack {
                Text("Title: ").bold()
                    + Text("\(post!.title)")
                ImageView(withURL: "http://localhost:8000/\(post!.path.replacingOccurrences(of: " ", with: "%20"))")
                    Text("Description: ").bold()
                        + Text("\(post!.description)")

            List(self.post2.filter { i in i.album_id == post.id }) { post       in
                Text("\(post.name ?? "title")")
                print(post2)
             }

但是由于我没有使用 onRecieve 我认为数据没有完全加载.在将数据传递到我的结构之前,我如何确保我拥有所有数据,或者确保我在我的结构中的变量 post2 中拥有它?

But since I'm not using onRecieve I don't think the data is loaded fully. How can I make sure I have all my data before passing it into my struct or make sure I have it in the variable post2 in my struct?

推荐答案

我想你想把 post2 声明为

I think you want to declare post2 as

 @Binding var post2:[PostById]

然后传入

 Album(post: post, post2: self.model2._postsById)

这就是 $ 的作用,但你不能在这里使用它.

Which is what $ does, but you can't use that here.

这篇关于如何确保变量中的数据在使用之前已加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆