Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Seamless CloudKit Sync with CoreData

License

NotificationsYou must be signed in to change notification settings

nofelmahmood/Seam

Repository files navigation

seamlogo

Pod VersionCarthage CompatiblePlatformLicense

Seam allows you to sync your CoreData Stores with CloudKit.

Topics

Features

  • Automatic mapping of CoreData Models to CloudKit Private Databases
  • Supports Assets
  • Background Sync
  • Conflict Resolution

Requirements

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 7.1+

Communication

Please read theContributing Guidelines before doing any of above.

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Seam into your Xcode project using CocoaPods, specify it in yourPodfile:

source'https://github.com/CocoaPods/Specs.git'platform:ios,'8.0'use_frameworks!pod'Seam','~> 0.6'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage withHomebrew using the following command:

$ brew update$ brew install carthage

To integrate Seam into your Xcode project using Carthage, specify it in yourCartfile:

github "Seam/Seam" ~> 0.6

Runcarthage update to build the framework and drag the builtSeam.framework into your Xcode project.

Usage

Add a Store type ofSeamStoreType to a NSPersistentStoreCoordinator in your CoreData stack:

letpersistentStoreCoordinator=NSPersistentStoreCoordinator(managedObjectModel: yourModel)letseamStore=try persistentStoreCoordinator.addPersistentStoreWithType(SeamStoreType,                                                                           configuration:nil,                                                                           URL: url, options:nil)as?Store

Observe the following two Notifications to know when the Sync Operation starts and finishes:

NSNotificationCenter.defaultCenter().addObserver(self, selector:"didStartSyncing:",                                                name: SMStoreDidStartSyncingNotification,                                                object: seamStore)NSNotificationCenter.defaultCenter().addObserver(self, selector:"didFinishSyncing:",                                                name: SMStoreDidFinishSyncingNotification,                                                object: seamStore)func didStartSyncing(notification:NSNotification){  // Prepare for new data before syncing completes}func didFinishSyncing(notification:NSNotification){  // Merge Changes into your context after syncing completes  mainContext.mergeChangesFromStoreDidFinishSyncingNotification(notification)}

Finally call sync whenever and wherever you want:

seamStore.sync(nil)

To trigger sync whenever a change happens on the CloudKit Servers. Subscribe the store to receive Push Notifications from the CloudKit Servers.

seamStore.subscribeToPushNotifications({ successfulinguard successfulelse{return}    // Ensured that subscription was created successfully})// In your AppDelegatefunc application(application:UIApplication, didReceiveRemoteNotification userInfo:[NSObject:AnyObject]){    seamStore.sync(nil)}

Attributes

All CloudKit Attributes are mapped automatically to your CoreData attributes with the exception ofCKAsset andCLLocation.

CKAsset and CLLocation can be used by setting the corresponding attribute as Transformable in your CoreData Model.

CloudKitCoreData
NSDateNSDate
NSDataNSData
NSStringNSString
NSNumberNSNumber
CKReferenceNSManagedObject
CKAssetTransformable
CLLocationTransformable

Transformable Attributes

CKAsset and CLLocation can be used in your CoreData model as Transformable attributes.

  1. To useCKAsset setTransformable as AttributeType andCKAssetTransformer as value transformer name for the attribute.

  1. To useCLLocation setTransformable as AttributeType andCLLocationTransformer as value transformer name for the attribute.

Relationships

CoreData RelationshipTranslation on CloudKit
To - oneTo one relationships are translated as CKReferences on the CloudKit Servers.
To - manyTo many relationships are not explicitly created. Seam only creates and manages to-one relationships on the CloudKit Servers.
Example -> If an Employee has a to-one relationship to Department and Department has a to-many relationship to Employee than Seam will only create the former on the CloudKit Servers. It will fullfil the later by using the to-one relationship. If all employees of a department are accessed Seam will fulfil it by fetching all the employees that belong to that particular department.

Note : You must create inverse relationships in your app's CoreData Model or Seam wouldn't be able to translate CoreData Models in to CloudKit Records. Unexpected errors and curroption of data can possibly occur.

Getting Started

Download the demo project. Run it and see the magic as it happens.

FAQ

tvOS Support ?

tvOS provides no persistent local storage. Seam uses SQLITE file to keep a local copy of your database which is not possible with tvOS.

Apps

Alist of Apps which are usingSeam.

Author

Seam is owned and maintained byNofel Mahmood.

You can follow him onTwitter andMedium

License

Seam is available under the MIT license. See theLICENSE file for more info.


[8]ページ先頭

©2009-2025 Movatter.jp