How to access CoreData model in today extension (iOS)
What you really want is to access your persistent store (most likely a SQLite database).
In order to achieve that, you need to configure App Groups and make sure that your host app configures the Core Data stack using your shared container (so your store is accessible in extension as well).
Something like:
NSString *containerPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:YOUR_SECURITY_APP_GROUP].path;
NSString *sqlitePath = [NSString stringWithFormat:@"%@/%@", containerPath, @"database.sqlite"];
Then in your extension just create persistent store coordinator with managed object contexts using database in shared container.
You can share your model (.momd) and managed object subclasses with extension just by making sure they are included in extension target as well.
Edit:
To add your model and managed object subclasses:
Make sure you have your app and extension targets
Click on your model file, and select both targets under 'Target Membership' on right-hand panel
Repeat the same with all your managed object subclasses
iOS today extension with core data
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!
}
...
}
iOS Access Core Data Auto Generated Classes inside Today Extension
You need to check the file named ReadHistory+CoreDataProperties.m
as a target to the extension also
How can I access core data details from Today Extensions(iOS 8)?
These links might helpful as they were to me
Swift - Sharing Core Data Model in App Group (w/ extension)
I can't use my core data model in two targets in a Swift project
http://blog.sam-oakley.co.uk/post/92323630293/sharing-core-data-between-app-and-extension-in-ios
When saving to CoreData from Today Extension, Data only accesable from Widget - NOT from Main Application
I finally fixed my issue <3.
It was so simple.
After hours and hours of testing testing crying and putting an axed through my MacBook ;) I found the thing thats what close killing me.
I testet if a simple Fetch Request in the Main App would get the added Items from the Today Extension. And that was working.
And than I saw it. In my Main App.
The NSFetchedResultController. I used an cache.
So now I notify the Main App when a new Weight was added via the widget - and than I call my refresh function.
func handleRefresh(_ refreshControl: UIRefreshControl) {
NSFetchedResultsController<NSFetchRequestResult>.deleteCache(withName: "weightCache")
do {
try fetchedResultsController.performFetch()
setupView()
self.tableView.reloadData()
} catch let error as NSError {
print("Fetching error: \(error), \(error.userInfo)")
}
refreshControl.endRefreshing()
}
So simple.
Just delete the cache.
Related Topics
How to Detect Swipe Gesture in iOS
Custom Alert (Uialertview) with Swift
Access Container View Controller from Parent iOS
Uitableview Within Uiscrollview Using Autolayout
Set Default iOS Local Notification Style for Application
How to Set iOS Status Bar Background Color in React Native
How to Programmatically Fake a Touch Event to a Uibutton
Missing Private Key in the Distribution Certificate on Keychain
Remove Println() for Release Version iOS Swift
Autolayout + Rtl + Uilabel Text Alignment
How to Fill a Uibezierpath with a Gradient
How to Get iOS Device MAC Address Programmatically
What Should Xcode 6 Gitignore File Include
In iOS, How to Create a Button That Is Always on Top of All Other View Controllers
Why the Skstorereviewcontroller Does Not Let Me Submit a Review
Swift - Check If a Timestamp Is Yesterday, Today, Tomorrow, or X Days Ago