I am trying to make a today extension for my ios app. That today extension will display the 'next' Course based on data saved with core data. I've been doing some research and I understand I have to share my persistentContainer with an appGroup. So I did :
public extension NSPersistentContainer {
func addToAppGroup(id: String) {
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: id) else {
fatalError("Shared file container could not be created.")
}
let storeURL = fileContainer.appendingPathComponent("\(self.name).sqlite")
let storeDescription = NSPersistentStoreDescription(url: storeURL)
self.persistentStoreDescriptions.append(storeDescription)
}
}
then in my core data stack :
internal class CoreDataContainer {
static var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "SchoolCompanion")
container.addToAppGroup(id: "group.com.Ce-dricLoneux.School-Companion")
container.loadPersistentStores(completionHandler: { (_, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = CoreDataContainer.persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
that files and the xcdatamodel are shared between both targets. At this point I thought I could access my core data from my extension but when i do a fetch request I don't get any result. The controllerDidChangeContent is never executed.
class TodayViewController: UIViewController, NCWidgetProviding {
private func configureFetchedResultsController() {
let request: NSFetchRequest<Course> = Course.fetchRequest()
request.sortDescriptors = []
self.fetchedResultsController = NSFetchedResultsController<Course>(fetchRequest: request, managedObjectContext: CoreDataContainer.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
self.fetchedResultsController.delegate = self
do {
try self.fetchedResultsController.performFetch()
} catch {
print(error.localizedDescription)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.extensionContext?.widgetLargestAvailableDisplayMode = .compact
self.configureFetchedResultsController()
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.newData)
}
}
// MARK: - FetchedResultsController Delegate
extension TodayViewController: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("content did change")
}
}
I tried to make a fetch request direclty too, buti get an empty array as result. Why does I don't get any result ?
Another thing : i used nsfetchedResult controller in order to refresh the view data each time the data is updated from the ios app so is that ok or should I use the widgetPerformUpdate method ? In that case we don't know when the today extension will be refreshed and it may dispay outdated data.
main documentation used : https://www.avanderlee.com/swift/core-data-app-extension-data-sharing/
Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.
The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.
ipa. An . ipa (iOS App Store Package) file is an iOS application archive file which stores an iOS app.
Get our help adding Core Data to your project So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we'll be deleting it when we're done. You'll see the “Use Core Data” checkbox on the project options screen, make sure it is checked.
Sharing your Core Data database with your Today extension, Action extension or Share extension is something quite common if you’re using Core Data as your database solution. Within the Collect App, we have a Share extension and an Action extension that both require the use of the same underlying persistent container.
The first step is to create a new project based on the Single View App template. Next, name the project, select your team name, and select “Use Core Data” and “Use CloudKit” checkboxes. Xcode generates some code in the AppDelegate.swift file to initialize the Core Data stack with this new class since iOS 13: NSPersistentCloudKitContainer.
Use Core Data to save your application’s permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.
Syncing data between iOS devices has become a simple process thanks to iCloud. Many iOS apps use iCloud to store data. This makes their information easier to synchronize across your devices when you install the same app.
You should subclass NSPersistentCloudKitContainer like below, returning the App Group URL for defaultDirectoryURL()
. Then in your CoreDataStack, use let container = GroupedPersistentCloudKitContainer(name: "SchoolCompanion")
. Also remove, your call to addToAppGroup(...)
. You will need to instantiate the GroupedPersistentCloudKitContainer in both the App and the Extension, you will also need to make sure the GroupedPersistentCloudKitContainer is linked to both Targets.
class GroupedPersistentCloudKitContainer: NSPersistentCloudKitContainer {
enum URLStrings: String {
case group = "group.com.yourCompany.yourApp"
}
override class func defaultDirectoryURL() -> URL {
let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: URLStrings.group.rawValue)
if !FileManager.default.fileExists(atPath: url!.path) {
try? FileManager.default.createDirectory(at: url!, withIntermediateDirectories: true, attributes: nil)
}
return url!
}
...
}
The url for your app group is here:
let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "SchoolCompanion)
The sqlite-file is there. You don't need Cloud container.
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