Part 9: Going Live
So this is the last part of this little series. In this part I swap in the CoreData/CloudKit store and display random things from there in the widgets.
Usual disclaimers apply, this is absolutely not production code, its purely a hacked together experiment to see what I need, don't need, can combine or needs to stay distinct :-)
So for this last commit we need to make a new ThingDataStore
to provide a set of shuffledThings
. I added this about ThingDataStore
in JustOneThingProvider.swift
All it does is pull all the PersistentThings
from the database and provide a way to return them shuffled. You'll notice I map the results using a property asThing
I added this as PersistentThingExtension.swift
and it provides a basic encapsulation. of the request (in case we want to filter in some way later) and a conversion from a PersistentThing
(a CoreData
ManagedObject
) to a Thing, our value type struct. It also hard codes what to do if either is null.
So our ThingCoreDataStore
provides a shuffled set of Things
, just like the other store, only these are loaded from CloudKit.
With all that in place we can now switch the Provider over to using the new DataStore and we're good to go!.
Lets make sure the app is using the right PersistenceController and we'll commit.
That's in the repo as "Part 9: Load the Things from CoreData".
Here's the widgets on a Phone screen...
and on the Mac Notification Center
One thing I have discovered and have not yet found a way round for, you'll see in the iPhone screenshot above. If you add the same size widget multiple times and don't have any ConfigurationIntent
parameters to change, each displayed widget is coming from the same instance. It's not possible to have each instance pull a different random Thing
. In essence there's only a single SystemWidget
instance for each size, that single instance pulls a random Thing and then its view is just blotted as many times as necessary.
This was kind of a pain for me as I quite liked the idea of having a random thing on each page of my phone. I could work around it by adding a purely synthetic integer configuration parameter "widget number" and changing it for each widget so they all become distinct and pull a different random Thing
, but that's beyond the scope of this little exercise.
I hope its been useful!