TL;DR - To simplify the whole description, how to go about implementing a non tableview based view controllers UI, when the core data object properties get frequently updated in the background. And with that the visual data representation needs to be updated immediately. [In tableview it's very easy to handle, you use a FRC and just reload the row when an the object updates, through FRCs delegate methods.]
I know about NSFetchedResultsController
and mapping through that to an User Interface. But that's only when the User Interface is list based.
In my case multiple hardwares of similar type are connected to my app through bluetooth (BLE), and each normally provides updates with frequency of 1 second generally. E.g Temperature, charge changes.
So here's an example of the UI flow,
List -> Details
Details
Details just represents a hardware related data (serial num, firmware version, manufacturing date etc), and it has 3 buttons named above which when tapped pushes to respective controllers.
The Entity model is also designed accordingly, here's a glimpse, I have a the primary entity let's say ABC
. Then ABC
has one-to-one relationship with other entities like HealthDetails
, Activity
, NotificationSetting
and HardwareDetails
. ABC
has few attributes like identifier
, connected
, name
, etc.
ListViewController
is a UITableViewController
and hence there I'm using an NSFetchedResultsController
. Now remaining all other view controllers are just normal view controllers, with just Title labels like 'last charged date', which is static and a 'description label' below it which shows the data. Other view controllers are also similarly populated with just buttons, labels etc.
Updating the list view controller is easy with NSFetchedResultsController
.
[When a row/cell is tapped by user, I just get the object using indexpath from FRC and inject it to the destination view controller.]
However the other view controllers are not table view based. So I am using notification to control other view controller UI updates.
For e.g when my BluetoothManager
receives an update from the hardware regarding a characteristic, I channelise the characteristic id and the data to my DatabaseHelper
, which then decodes the data and inserts into the respective managed object, and at this point I just fire a notification with the identifier of the object which got updated.
All the UIViewController
s in the hierarchy which are in the navigation stack are subscribed as observers. And if the identifier in the notification matches the entity object being shown currently then I refresh the UI. Currently all this works fine.
But, I feel it's very clumsy, handling lots of notifications. Is this the only way or are there any better methods existing out there to tightly couple individual core data models to UIs?
I see few comments which suggests to handle all through FRC only, but I'm not sure if FRC can be used in a non tableview based UI representation. I have searched and haven't found much. If anyone knows any tutorial/blog with even theoretical description that would be great help.
Another thing the suggestions on FRC to handle all, I didn't get it completely, do I need to place one FRC per view controller? Isn't there any other way?
You shouldn't lose anything. The delegate on the NSFetchedResultsController will tell you any time something is updated. If you have a visible UI, then your UI will update. If you don't have a visible UI, or don't want one, you can still link that FRC to your specific NSManagedObject subclass for the health details and then take whatever action is appropriate based on the fact that something changed.
Depending on what you're doing, it may be as simple as implementing controllerDidChangeContent(_:)
If that doesn't answer your concern, you're going to have to provide more details as to what the specific issue is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With