Latest 0.1.0
Homepage https://github.com/vadeara/CDShare
License MIT
Platforms ios 11.0
Authors

CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 11.0+ 
Xcode 10

Installation

CDShare is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'CDShare'

Usage

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.

CDShare will answer the question:
How do we share CoreData between 2*n application, where n >= 1?

Before we will direct to the subject, let have a short introduction for each part that we will touch in this tutorial.

What is CoreData?

CoreData is a framework created by Apple, is used for object layer modeling management.
It provides a general solution for functions associated with the object lifecycle, object management, and persistence.
What should be mentioned in this framework are the following classes:

NSPersitentStoreCoordinator / NSPersitentStoreContainer
NSManagedObjectModel
NSManagedObjectContext
NSManagedObject

alt tag
alt tag

iOS Application structure:

In order to share CoreData between 2*n applications, we have some impediments.

The main problem in iOS is that each application is totally isolated from other applications through a Sandbox.
This limits are interprocess communication and accessing directories/files between applications, normally we do not have access to applications in the directory structure.

Directory structure of an application:

Application directory structure with iCloud:
alt tag

Application directory structure without iCloud:
alt tag

AppGroup

First of all, you need to create app groups for your application. Go to Apple Developer Member Center and register app group. Fill the description and identifier and follow the instructions.

alt tag
alt tag
alt tag

After that when you will create an identifier for an application or an extension, don’t forget to enable the service App Groups.

alt tag

Then go to the application or the extension and edit services. It’s really simple, see the next screenshots:

alt tag
alt tag
alt tag
alt tag
alt tag

And please, perform this procedure for all extensions of the group. It’s all settings, now open the Xcode and let’s go to write code.

In the Xcode for the each target enable App Groups in target settings.

We have support for access and communication between applications, it needs to belong to the same group.

Each application when running is a separate process, and even if the applications are part of the same group,
they do not access each other’s directories, but they have access to the Shared Container that has a Sandbox-like directory structure.

Process structure between Applications

alt tag

CDShare Logic

We will create a folder in the Shared Container where we will save SQLite and we will have one folder for each application, the folder name will be the bundle ID of each application. In these folders, we will save files with a unique name in which they will contain the payload received in the notification with the name CoreDataSaveNotification.

Framework structure

alt tag

iOS Application states

Applications in IOS have 5 states, these states are shown in the schematic below:

alt tag

FolderWatcher will work as long as the application is active when it enters the background, fileDescriptor EventHandler is put in a queue when the application will come in the active state, events from the queue will be fired.
The fileDescriptor does it automatically you do not have to make any extra handlers.

Example description

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 example will be based on another tutorial that you can find here.

The logic of the framework looks like this:

  1. 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
}
  1. Create the CoreDataShareConfiguration instance.
    let context: NSManagedObjectContext = ...
    let configuration = try! CoreDataShareConfiguration(ApplicationGroupInfoModel(), readingContext: context, writingContext: context)
  2. Create CoreDataShare instance.
    let coreDataShare = try! CoreDataShare(configuration: configuration, viewContext: context)
  3. Do not forget to add the class type that you want to update/reload objects in memory.
    coreDataShare.sharedInScopeEntityNames = [String(describing: Counter.self)]
  4. Add the notification of .CoreDataShareDidSave:.
    let selector = #selector(reloadObjects)
    NotificationCenter.default.addObserver(self, selector: selector, name: .CoreDataShareDidSave, object: nil)

Author

vodaion, [email protected]

License

CDShare is available under the MIT license. See the LICENSE file for more info.
Application documentation.

Latest podspec

{
    "name": "CDShare",
    "version": "0.1.0",
    "summary": "A short description of CDShare.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/vadeara/CDShare",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "vodaion": "[email protected]"
    },
    "source": {
        "git": "https://github.com/vadeara/CDShare.git",
        "tag": "0.1.0"
    },
    "swift_version": "4.2",
    "platforms": {
        "ios": "11.0"
    },
    "source_files": "CDShare/CDShare/Classes/**/*"
}

Pin It on Pinterest

Share This