Best performance to passing data between controllers - ios

I’am studying RxSwift and have this one case.
From tableView cell, I go to info restaurant, and if I change the rate for this restaurant, I want to change this rate in tableView too, but I don’t want to update tableView, because restaurants are downloaded from the backend, cause restaurants data is not loaded every time when viewWillApear is called. Now I’m using cloujers, in prepareForSegue, I’m setting a new rate in cell from its info. I read that the cloujers are used in a lot of resources, can this be done with the help of RxSwift or another best way?

This is best library to work with collection view and table view in RxSwift
https://github.com/RxSwiftCommunity/RxDataSources

Related

ViewModel for UITableViewCell: Database operations "allowed"?

I am trying to understand how to correctly use the MVVM pattern in iOS. Lets assume we have a music player app with a playlist like in the following sketch:
I have a PlaylistViewController and a PlaylistViewModel. Furthermore the cells (SongTableViewCell) have a view model SongCellViewModel which holds the data (song name,..). If the user presses the X button on the TableViewCell I have to delete the song from the database. The cell is calling the following function in the SongCellViewModel:
func deleteSongFromPlaylist() {
DatabaseService.shared.deleteSong(self.song)
}
My question: Is it good to do it that way? It doesn't feel right to initiate database operations in the view model of the TableViewCell.
I suggest moving such operations to PlaylistViewModel and do all operations from there. It also simplifies implementation of later improvements like showing activity indicator if needed or reloading certain cells or views from there.
I think that SongCellViewModel should have a actionBlock which will take enum with associated values - for example .select(song) .delete(song) - and this way you can easily communicate between PlaylistViewModel and SongCellViewModel.
One more suggestion is to use Dependency Injections instead of using shared for all type of services.

How to create a table in swift with segue?

I'm brand new to Swift but I want to create an app that has a table and each entry in that table will lead to a new screen (but this new screen is the same for all the table entries) but depending on which table cell you click on, that screen has different information posted on it.
What are the steps I need to do to complete this? I have my story board I'm just not sure how to put this all together with code
Thanks in advance!
Well your question is very broad and not very specific. Therefore it is not easy to give a helpful answer. What I would do:
Create a UITableViewController that holds your "table".
Define a UITableViewControllerDelegate for the UITableView that will be informed about UserInteractions (especially when the user
didSelect a certain row of the table).
Based on the specific row (that was selected) you can create a second UIViewController class the shows your intended information.
I can not show some code samples because your question is to broad and things depend on a lot of things (especially on the kind of data you want to show) and how you implement your UIElements on the ViewControllers in InterfaceBuilder.
And if you are not yet familiar with the concept of a UITableViewController and its Delegate, than you should find some tutorials first about that basic technique in developing iOS apps.
You want to use the master/detail pattern. I suggest doing a search on that.

iOS using UITableView reloadData vs endUpdates

I am writing an app with a basic UITableView that shows NSManagedObjects that are loaded from CoreData.
There is also a background process that is running and notifies the viewController using NSNotificationCenter of any changes to the CoreData NSManagedObject (IE: Using NSInserted/Updated/DeletedObjectsKey). I have a few questions regarding the best way to update the tableView after the view receives the notification of changed data.
Is it better to call reloadData on the tableView or figure out a diff on the data and do the inserts/deletes/updates inside a tableview.BeginUpdates() - tableView endUpdates()?
Is it a valid practice to refresh the tableView while a user is interacting with the tableView?
Is there an easy way that I am missing to do a diff between two arrays of the CoreData NSManagedObjects and apply to the tableView?
I hope I am not overcomplicating things
It depends. If you want just refresh data without any animation then use reloadData. In the case when you build up an user-friendly smooth data changing animations, it would be better to calculate the diff and insert/delete rows with some beautiful animation
User is not interacting with tableView infinitely. You may track when table is not dragging and update the data, but refreshing view to show actual data is common practice today. For example, at the web it is AJAX technology. Also, as I mentioned in 1, use animation to attract user's attention to new data
As it mentioned in the comments, in your case using NSFetchedResultsController would be better

How to relate data to a specific row in a UITableView

What I basically want is to show the user's name in the first tableView but have a disclosure indicator (segue) to go to a second tableView which will show the history of the selected user. I know how to load data into a table and how to segue from one row to the detail table but I just don't know how to show different data for each user. I know this may be out of my scope of knowledge at the moment but I would like to give it a try.
1- What would be the best way to store the data for each user (history), an array per user?
2- How can I relate data to a specific user so it loads when the user's name is touched?
3- How can I capture the row being selected?
Any suggestions will be appreciated.
Honestly, you shouldn't worry about it being outside of your scope of knowledge, as that's how people learn. You will need to do some amount of work, however, and that part can be a little difficult.
Luckily, what you're trying to do is a very well documented function and Xcode even provides a template for master -> detail views.
Check out this wonderful tutorial by Ray Wenderlich on creating a simple master-detail application: http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1
His other tutorials will also be immensely helpful to beginning iOS development: http://www.raywenderlich.com/tutorials

Multiple TableViewControllers & Segues

I have a TableView that is populated with data. When a user taps a row, a new TableView opens with more data to choose from. This data changes based on the row the user taps. Much like an application where the user chooses from a list of car makes, and then models. Is it possible to handle a situation like that with a TableViewController for the car makes, and 1 TableViewController for the models? Or would there be 1 TableViewController for the makes, and then a separate TableViewController for each make's set of models? I don't know the best way to approach this :-( I'm making an app of my own which is very similar in structure and I can't seem to find any documentation on how to accomplish this. Thanks in advance.
No, your models controller would serve for all makes. It would be passed an ID of the make requested and then fetch the necessary data accordingly.
Something like this might be of some help:
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewAndDataModel/TableViewAndDataModel.html
But I think the foundation of your problem is not necessarily to be found in a working example of your exact needs, but more in general tutorials for ios development.
Sorry if I have assumed you're just beginning ios development. I might be able to improve this answer if a more specific need is clarified (like how do I pass the ID to the model controller) etc
In general - this approach would utilize a separate UITableViewController for each data level and use a UINavigationController to handle the transitions between the different view controllers.
This process used to involve a lot of boilerplate code, but it is now aided by StoryBoards. You can setup each level in a storyboard and handle the transitions and setup data for the next view in performSegueWithIdentifier:.
You can see a tutorial here on using StoryBoards: http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-uitableview-tutorial.html
You can see another Sample From Apple that demonstrates the data drill down with a Storyboard: http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007416-Intro-DontLinkElementID_2

Resources