This work was inspired by how Apple iWork office suite applications work in iOS and a specific feature of how the documents are sharing between apps even in offline mode.
Before to go direct to the subject, let's have a short introduction for each part that is touched in this tutorial.
The contains documentation, samples, contributions, etc. Start there.
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 11.0+
Xcode 10
CDShare is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CDShare'
Under the Example folder, you will find a project of how to use the CDShare.framework
.
The project will contain a showcase for an Application and an Extension for it and they will share the CoreData.
The logic of the framework looks like this:
- Create a class/struct that implement
ApplicationGroupInfo
protocol.
struct ApplicationGroupInfoModel: ApplicationGroupInfo {
var group: ApplicationIdentifier = ApplicationIdentifierModel(identifier: "group.voda.the.cdshare")
var reading: ApplicationIdentifier = ApplicationIdentifierModel(identifier: "com.CDShareExample")
var writing: [ApplicationIdentifier] = [ApplicationIdentifierModel(identifier: "com.CDShareExample.CDShareExampleToday")]
}
You can have diferent context for reading and writing, for the sake of the example I did use the same context.
Each parameter of the ApplicationGroupInfo
protocol require to be descedent of the ApplicationIdentifier
protocol.
struct ApplicationIdentifierModel: ApplicationIdentifier {
var identifier: String
}
- Create the
CoreDataShareConfiguration
instance.
let context: NSManagedObjectContext = ...
let configuration = try! CoreDataShareConfiguration(ApplicationGroupInfoModel(), readingContext: context, writingContext: context)
- Create
CoreDataShare
instance.
let coreDataShare = try! CoreDataShare(configuration: configuration, viewContext: context)
- Do not forget to add the class type that you want to update/reload objects in memory.
coreDataShare.sharedInScopeEntityNames = [String(describing: Counter.self)]
- Add the notification of
.CoreDataShareDidSave:
.
let selector = #selector(reloadObjects)
NotificationCenter.default.addObserver(self, selector: selector, name: .CoreDataShareDidSave, object: nil)
vodaion, vanea.voda@gmail.com
CDShare is available under the MIT license. See the LICENSE file for more info. Application documentation.