iOS Today Extension with Core Data

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:

1. Make sure you have your app and extension targets

  1. Make sure you have your app and extension targets

    2. Click on your model file, and select both targets under 'Target Membership' on right-hand panel

  2. Click on your model file, and select both targets under 'Target Membership' on right-hand panel

    3. Repeat the same with all your managed object subclasses

  3. 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

Sample Image



Related Topics



Leave a reply



Submit