Update a value in a getx controller before it has been rendered - flutter-getx

I have a long list of items (objects) and each item has a getx controller.
Get.put(ItemController(itemModel), tag: itemModel.value.id.toString()) is set in the ListView.builder() when the item tile is created, but it seems that the controller only actually gets created when the tile is displayed and only the first 10 or so are displayed initially.
I then need to update the values for all (many) of the items.
When I attempt to update the values in the item controllers, it can't "find" the controllers (because they have not yet been created).
Is it ok to use Get.put() again for the same item? Or is there a better way to access these item controllers?

You need to create a method which loops through your list item and puts your dependencies for each item.
And you need to call that method on the build method, before ListView.builder.
GetX only creates the dependencies when they are needed. Like, if you just 'Get.put(Something())', it will not necessarily be called at the moment of calling Get.put. Usually the dependencies gets created when that dependency is being used for the first time. So GetX will create Something when something.someOtherThing is called.

Related

Reference master controller from detail controller

In the top of my detailcontroller I have a button to show a list of selected items from various options on the detail controller.
I want to initialize and keep the model, the popover and the list objects in the mastercontroller. How can I get a reference to my mastercontroller to reuse these objects?
Sharing data between views is easiest using a model tied to the component. If you make sure that everything that's selected in the master model is contained in that model, you will be able to use the data from the model in the detail view again.
Pulling data directly from another controller would go against the MVC paradigm and is not recommended (although not impossible, using e.g. the sap.ui.getCore().byId() method). I would not go there unless you have a clear view of the consequences.

Kendo UI Mobile MVVM - How to handle two list views that navigate to a single detail view?

I have two ListViews (in separate views). These views are bound to separate view-models but the ListViews contain the same entity type. Both views allow the user to select an item and navigate to it's detail/edit view.
What do I need to do to share this detail view between the two list views?
Here is what I have tried:
Assign the selected item to a property in the detail view's view-model
This initially appeared to work but actually breaks Kendo MVVM. Since the item is in the list view's view-model, assigning it to a property in another view-model causes problems.
Refresh data in each view's show event
While this almost works, it has a couple problems. 1) Getting fresh data all the time can be slow. 2) When saving changes in the detail view and navigating back to the list view, the save is async, so there is no guarantee that those changes will have been persisted before the call for ListView data. This also negates one of the benefits of MVVM and observables.
Share the view-model across views
The examples I have seen that have a list and detail view, have both views sharing a view-model with a selectedItem property. This is not possible in my particular case because I have two list views that navigate to the same detail view - not to mention that I prefer to have a separate view-model for each view so that the view-models don't become a huge mess. Am I supposed to have all views share a single view-model?
What am I missing?
Maybe you could extract the observable model to a plain object model with the toJSON() method, and then create a new observable model from it by wrapping it again. This should clear the existing bindings and avoids the conflicts you've found in your first approach.
var model = kendo.observable( otherModel.toJSON() );

Sharing a core data database between two instances of a table view controller

I'm making a shopping cart. In one tab, I have a table view controller with all the items. I have another tab, I have a shopping cart that has an 'add item to cart' button which segues to another table view controller whose class I set the same as the table view controller of the items in the other tab.
But the problem is, the one from the segue does not load the rows anymore, so I get a blank cells. How do I fix this?
Does my question have anything to do with passing NSManagedObjects among objects?
You have to be very careful passing ManagedObjects between threads. dispatch call is likely to do so. Instead, contain the processing to one thread per ManagedObjectContext. I prefer using a specific worker thread that main calls which then dispatches the messages asynchronously to private methods.
This may or may not be the cause of your particular problem, but I've had significant issues with Core Data working across threads that are likely to at the very least pop up later for you.

How can I get a method to call in a class while the user is in a view of another class?

I have an iPhone app based on a tabBar and tableViews. I want the user to be able to click on one tab and access options for filtering the data in the initial tableView.
The problem I'm having is that while the user is selecting filter criteria, I want the main table (not visible) to update. The reason this is important is that I want to show how many cells are still in the table as it is being filtered in the navigation bar.
Currently, the method for filtering the main table (-handleFilter) is called in the viewWillAppear method of my rootViewController class. How can I call this method from my "searchOptions" class?
Thanks for the help!
It sounds like you're conflating too much between your model and your controllers (assuming you're following the MVC design pattern). The other controllers besides the main table should be able to query the model themselves to display the count information without asking the main table controller.
I could be misunderstanding something though, a little more information on what data you're using and how it's being filtered in the controllers attached to the other tab bar items would help.
The most straightforward way would be to give the options controller a pointer to the list controller. Then you can call the method directly.
Other options include defining a method/property on some global object (like your app delegate) to access the list controller from elsewhere in the app, and using a more decentralized mechanism like NSNotificationCenter to pass that information around (rather an a method call), or relying on the model itself to notify all of the controllers accessing it when it changes (possibly using Key-Value Observing, or an explicit delegate protocol).

Best way to re-use a UITableView with dynamic data?

My application has a series of table views based on some hierarchical data. For example, the user selects "Browse by XYZ" on my CategoryListController, and then I load my DocumentListController based on that selection.
Right now I'm getting the list through a web service that returns some JSON data, but that's beside the point. I could retrieve this dynamic list from a SQLite database, and I would have the same underlying challenge.
The problem here is that since the list of items for my table view in DocumentListController will change depending on which selection the user tapped on, I have to load my list after the table view is displayed.
Right now I'm using -viewWillAppear: to trigger this "refresh" of the data items from my web service. I was wondering if this is the best way to be doing this refresh, or if I should look into using a different method. I tried using -viewDidLoad but that method gets called only once for the DocumentListController, and I have to check at every invocation if the "selection" has changed, and if so, I need to call my web service again.
What's the best way to do something like this?
Since you retrieve the data through the network, I would suggest to post a notification after you process it. Your DocumentListController should register for that notification and invoke its table view's reloadData in the notification handler.
Another approach, if the data will always be there when displaying the view, is what you've suggested - update table view in viewWillAppear.