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!
}
...
}
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
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.
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
Related Topics
Xcode 4.2 with Arc: Will My Code Run Even on iOS Devices with Firmware Older Than 5.0
How to Use Sketch UI Elements Directly into Xcode
Background Image Not Displaying Correctly in iOS
iOS 8 Code Working on iPhone 5S But Not iPhone 5
How to Set Gradient Background Transparency on iOS Safari
I Get Nil When Using Nsdateformatter in Swift
Dynamic Cell Height with Sdwebimage
iOS How to Get a List of Already Purchased Products
Launching App from Sms Link or Email Link
Swift Unrecognized Selector Sent to Instance - What Am I Missing
How to Prevent Duplicates in Realmswift List
How to Override Convenience Init in Uialertcontroller for Swift
Hls Video Streaming on iOS/Safari
How to Check a App Was Installed or Updated
Check the Position of the Xcuielement on Screen While Testing iOS Application Using Xctest
Selecting All The Items in UIcollectionview iOS, Even The Cells That Are Not Visible
How to Make Collage of Images According to Different Shapes
Why [Uiscreen Mainscreen].Bounds] Is Not Returning Full Screen Size