How to make sure data in variable is loaded before

2020-05-09 01:10发布

问题:

So I have an .onRecieve method

.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")]

... plus more

Which seems to be all my data

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)) {

on line: 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)
             }

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?

回答1:

try this:

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)")

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


回答2:

I think you want to declare post2 as

 @Binding var post2:[PostById]

And then pass in

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

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



标签: swift swiftui