iOS Swift: TableView Data Source without using RxCocoa





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Evening, in my application I do not want to use RxCocoa and I'm trying to conforming to tableview data source and delegate but I'm having some issues.



I can't find any guide without using RxCocoa or RxDataSource.



In my ViewModel in have a lazy computed var myData: Observable<[MyData]> and I don't know how to get the number of rows.



I was thinking to convert the observable to a Bheaviour Subject and then get the value but I really don't know which is the best prating to do this










share|improve this question































    0















    Evening, in my application I do not want to use RxCocoa and I'm trying to conforming to tableview data source and delegate but I'm having some issues.



    I can't find any guide without using RxCocoa or RxDataSource.



    In my ViewModel in have a lazy computed var myData: Observable<[MyData]> and I don't know how to get the number of rows.



    I was thinking to convert the observable to a Bheaviour Subject and then get the value but I really don't know which is the best prating to do this










    share|improve this question



























      0












      0








      0








      Evening, in my application I do not want to use RxCocoa and I'm trying to conforming to tableview data source and delegate but I'm having some issues.



      I can't find any guide without using RxCocoa or RxDataSource.



      In my ViewModel in have a lazy computed var myData: Observable<[MyData]> and I don't know how to get the number of rows.



      I was thinking to convert the observable to a Bheaviour Subject and then get the value but I really don't know which is the best prating to do this










      share|improve this question
















      Evening, in my application I do not want to use RxCocoa and I'm trying to conforming to tableview data source and delegate but I'm having some issues.



      I can't find any guide without using RxCocoa or RxDataSource.



      In my ViewModel in have a lazy computed var myData: Observable<[MyData]> and I don't know how to get the number of rows.



      I was thinking to convert the observable to a Bheaviour Subject and then get the value but I really don't know which is the best prating to do this







      ios swift uitableview datasource rx-swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 22:40







      Miotz

















      asked Jan 3 at 22:26









      MiotzMiotz

      9741927




      9741927
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to create a class that conforms to UITableViewDataSource and also conforms to Observer. A quick and dirty version would look something like this:



          class DataSource: NSObject, UITableViewDataSource, ObserverType {
          init(tableView: UITableView) {
          self.tableView = tableView
          super.init()
          tableView.dataSource = self
          }

          func on(_ event: Event<[MyData]>) {
          switch event {
          case .next(let newData):
          data = newData
          tableView.reloadData()
          case .error(let error):
          print("there was an error: (error)")
          case .completed:
          data =
          tableView.reloadData()
          }
          }

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return data.count
          }

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let item = data[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
          // configure cell with item
          return cell
          }

          let tableView: UITableView
          var data: [MyData] =
          }


          Make an instance of this class as a property of your view controller.
          Bind your myData to it like:



          self.myDataSource = DataSource(tableView: self.tableView)
          self.myData
          .bind(to: self.myDataSource)
          .disposed(by: self.bag)


          (I put all the selfs in the above to make things explicit.)



          You could refine this to the point that you effectively re-implement RxCoca's data source, but what's the point in that?






          share|improve this answer
























          • I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

            – Miotz
            Jan 4 at 8:30











          • That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

            – Daniel T.
            Jan 4 at 12:32











          • thank you, I was just curious about your personal opinion

            – Miotz
            Jan 4 at 13:38













          • Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

            – Daniel T.
            Jan 4 at 13:43












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030659%2fios-swift-tableview-data-source-without-using-rxcocoa%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You need to create a class that conforms to UITableViewDataSource and also conforms to Observer. A quick and dirty version would look something like this:



          class DataSource: NSObject, UITableViewDataSource, ObserverType {
          init(tableView: UITableView) {
          self.tableView = tableView
          super.init()
          tableView.dataSource = self
          }

          func on(_ event: Event<[MyData]>) {
          switch event {
          case .next(let newData):
          data = newData
          tableView.reloadData()
          case .error(let error):
          print("there was an error: (error)")
          case .completed:
          data =
          tableView.reloadData()
          }
          }

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return data.count
          }

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let item = data[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
          // configure cell with item
          return cell
          }

          let tableView: UITableView
          var data: [MyData] =
          }


          Make an instance of this class as a property of your view controller.
          Bind your myData to it like:



          self.myDataSource = DataSource(tableView: self.tableView)
          self.myData
          .bind(to: self.myDataSource)
          .disposed(by: self.bag)


          (I put all the selfs in the above to make things explicit.)



          You could refine this to the point that you effectively re-implement RxCoca's data source, but what's the point in that?






          share|improve this answer
























          • I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

            – Miotz
            Jan 4 at 8:30











          • That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

            – Daniel T.
            Jan 4 at 12:32











          • thank you, I was just curious about your personal opinion

            – Miotz
            Jan 4 at 13:38













          • Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

            – Daniel T.
            Jan 4 at 13:43
















          1














          You need to create a class that conforms to UITableViewDataSource and also conforms to Observer. A quick and dirty version would look something like this:



          class DataSource: NSObject, UITableViewDataSource, ObserverType {
          init(tableView: UITableView) {
          self.tableView = tableView
          super.init()
          tableView.dataSource = self
          }

          func on(_ event: Event<[MyData]>) {
          switch event {
          case .next(let newData):
          data = newData
          tableView.reloadData()
          case .error(let error):
          print("there was an error: (error)")
          case .completed:
          data =
          tableView.reloadData()
          }
          }

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return data.count
          }

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let item = data[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
          // configure cell with item
          return cell
          }

          let tableView: UITableView
          var data: [MyData] =
          }


          Make an instance of this class as a property of your view controller.
          Bind your myData to it like:



          self.myDataSource = DataSource(tableView: self.tableView)
          self.myData
          .bind(to: self.myDataSource)
          .disposed(by: self.bag)


          (I put all the selfs in the above to make things explicit.)



          You could refine this to the point that you effectively re-implement RxCoca's data source, but what's the point in that?






          share|improve this answer
























          • I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

            – Miotz
            Jan 4 at 8:30











          • That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

            – Daniel T.
            Jan 4 at 12:32











          • thank you, I was just curious about your personal opinion

            – Miotz
            Jan 4 at 13:38













          • Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

            – Daniel T.
            Jan 4 at 13:43














          1












          1








          1







          You need to create a class that conforms to UITableViewDataSource and also conforms to Observer. A quick and dirty version would look something like this:



          class DataSource: NSObject, UITableViewDataSource, ObserverType {
          init(tableView: UITableView) {
          self.tableView = tableView
          super.init()
          tableView.dataSource = self
          }

          func on(_ event: Event<[MyData]>) {
          switch event {
          case .next(let newData):
          data = newData
          tableView.reloadData()
          case .error(let error):
          print("there was an error: (error)")
          case .completed:
          data =
          tableView.reloadData()
          }
          }

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return data.count
          }

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let item = data[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
          // configure cell with item
          return cell
          }

          let tableView: UITableView
          var data: [MyData] =
          }


          Make an instance of this class as a property of your view controller.
          Bind your myData to it like:



          self.myDataSource = DataSource(tableView: self.tableView)
          self.myData
          .bind(to: self.myDataSource)
          .disposed(by: self.bag)


          (I put all the selfs in the above to make things explicit.)



          You could refine this to the point that you effectively re-implement RxCoca's data source, but what's the point in that?






          share|improve this answer













          You need to create a class that conforms to UITableViewDataSource and also conforms to Observer. A quick and dirty version would look something like this:



          class DataSource: NSObject, UITableViewDataSource, ObserverType {
          init(tableView: UITableView) {
          self.tableView = tableView
          super.init()
          tableView.dataSource = self
          }

          func on(_ event: Event<[MyData]>) {
          switch event {
          case .next(let newData):
          data = newData
          tableView.reloadData()
          case .error(let error):
          print("there was an error: (error)")
          case .completed:
          data =
          tableView.reloadData()
          }
          }

          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return data.count
          }

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let item = data[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
          // configure cell with item
          return cell
          }

          let tableView: UITableView
          var data: [MyData] =
          }


          Make an instance of this class as a property of your view controller.
          Bind your myData to it like:



          self.myDataSource = DataSource(tableView: self.tableView)
          self.myData
          .bind(to: self.myDataSource)
          .disposed(by: self.bag)


          (I put all the selfs in the above to make things explicit.)



          You could refine this to the point that you effectively re-implement RxCoca's data source, but what's the point in that?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 2:03









          Daniel T.Daniel T.

          14.5k22734




          14.5k22734













          • I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

            – Miotz
            Jan 4 at 8:30











          • That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

            – Daniel T.
            Jan 4 at 12:32











          • thank you, I was just curious about your personal opinion

            – Miotz
            Jan 4 at 13:38













          • Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

            – Daniel T.
            Jan 4 at 13:43



















          • I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

            – Miotz
            Jan 4 at 8:30











          • That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

            – Daniel T.
            Jan 4 at 12:32











          • thank you, I was just curious about your personal opinion

            – Miotz
            Jan 4 at 13:38













          • Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

            – Daniel T.
            Jan 4 at 13:43

















          I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

          – Miotz
          Jan 4 at 8:30





          I'm afraid to get away too much from apple standard pattern and to write code that just few understands.. So you think I should use rxCocoa and maybe rxDataSource?

          – Miotz
          Jan 4 at 8:30













          That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

          – Daniel T.
          Jan 4 at 12:32





          That's up to your team/boss, not me. I'm just saying that if you aren't going to use RxCocoa then it would be silly to go through the effort to re-implement it. The above is not a re-implementation, it's a one off class that is only suitable for a specific purpose.

          – Daniel T.
          Jan 4 at 12:32













          thank you, I was just curious about your personal opinion

          – Miotz
          Jan 4 at 13:38







          thank you, I was just curious about your personal opinion

          – Miotz
          Jan 4 at 13:38















          Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

          – Daniel T.
          Jan 4 at 13:43





          Personally, I'm all in. I think reactive approaches to programming are the future. Thirty years ago, I jumped onboard the OO bandwagon when lots of people were still thinking it was a fad. I see Reactive programming as the next big paradigm shift. IMHO, twenty years from now, everything is going to be reactive.

          – Daniel T.
          Jan 4 at 13:43




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030659%2fios-swift-tableview-data-source-without-using-rxcocoa%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas