siegesmund / SwiftDDP

A Meteor client, written in Swift
MIT License
145 stars 60 forks source link

closure is triggered before subscription is ready #89

Open jaddoescad opened 7 years ago

jaddoescad commented 7 years ago
            let users = MeteorCollection<User>(name: "users")
      Meteor.subscribe("users") , callback: {
                        var id = Meteor.client.userId()
                        self.navigationController!.performSegue(withIdentifier: "LoginSuccess", sender: nil)
                        let json = JSON(users.findOne(id!)?.services)
                        print(json["facebook"]["id"])
                    })

The following code above is supposed to retrieve the user's information when I subscribe to "users". The problem is sometimes I get null for user information. I'm really not sure why?

this is my server code:

  Meteor.publish("users", function () {
if (this.userId) {
    return Meteor.users.find({_id: this.userId}               
    {fields: {'services.resume': 0}});
  } else {
    this.ready();
  }
});

How can I make sure that the closure is only triggered once I am ready to receive everything.

mastertrunk commented 7 years ago

very annoying issue, when I subscribe to my messages, sometimes i get info, sometimes i dont. my guess is somehow it thinks that there is nothing to load, i'm not sure why. you should try and look at meteor-ios,never happened to me there.

mastertrunk commented 7 years ago

a workaround is to also use abstract collection notifications, it may give them to you one by one, but it mostly never fails

siegesmund commented 7 years ago

I can't tell what you're doing from the code you've provided. But I see a number of potential problems. First, you shouldn't put your navigation logic inside subscription callback. That's opening up a number of potential problems and is going to make this harder to reason about. Second, you shouldn't define your collections inside your view controllers. When you navigate away users is going to disappear.

What do your DDP logs look like on the client. They'll show you exactly what is being transmitted from the server.