Giter Site home page Giter Site logo

sync's Introduction

Hyper Sync™

Version License Platform

Sync eases your every day job of parsing a JSON response and getting it into Core Data. It uses a convention over configuration paradigm to facilitate your workflow.

  • Handles operations in safe background threads
  • Thread safe saving, we handle retrieving and storing objects in the right threads
  • Diffing of changes, updated, inserted and deleted objects (which are automatically purged for you)
  • Auto-mapping of relationships (one-to-one, one-to-many and many-to-many)
  • Smart-updates, only updates your NSManagedObjects if the server values are different (useful when using NSFetchedResultsController delegates)
  • Uniquing, Core Data does this based on objectIDs, we use your remote key (such as id) for this

Interface

+ (void)changes:(NSArray *)changes
  inEntityNamed:(NSString *)entityName
      dataStack:(DATAStack *)dataStack
     completion:(void (^)(NSError *error))completion
  • changes: JSON response
  • entityName: Core Data's Model Entity Name (such as User, Note, Task)
  • dataStack: Your DATAStack

Example

Model

Model

JSON

[
  {
    "id": 6,
    "name": "Shawn Merrill",
    "email": "[email protected]",
    "created_at": "2014-02-14T04:30:10+00:00",
    "updated_at": "2014-02-17T10:01:12+00:00",
    "notes": [
      {
        "id": 0,
        "text": "Shawn Merril's diary, episode 1",
        "created_at": "2014-03-11T19:11:00+00:00",
        "updated_at": "2014-04-18T22:01:00+00:00"
      }
    ]
  }
]

Sync

[Sync changes:JSON
inEntityNamed:@"User"
    dataStack:dataStack
   completion:^{
       // New objects have been inserted
       // Existing objects have been updated
       // And not found objects have been deleted
    }];

More Examples

Getting Started

Installation

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

pod 'Sync'

Requisites

Core Data Stack

Replace your Core Data stack with an instance of DATAStack.

self.dataStack = [[DATAStack alloc] initWithModelName:@"Demo"];

Then add this to your App Delegate so everything gets persisted when you quit the app.

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self.dataStack persistWithCompletion:nil];
}

Primary key

By default Sync uses id from the JSON and remoteID from Core Data as the primary key. You can mark any attribute as primary key by adding hyper.isPrimaryKey and the value YES.

Custom primary key

Attribute mapping

Your attributes should match their JSON counterparts in camelCase notation instead of snake_case. For example first_name in the JSON maps to firstName in Core Data and address in the JSON maps to address in Core Data.

There are two exceptions to this rule:

  • ids should match remoteID
  • Reserved attributes should be prefixed with the entityName (type becomes userType, description becomes userDescription and so on). In the JSON they don't need to change, you can keep type and description for example. A full list of reserved attributes can be found here

If you want to map your Core Data attribute with a JSON attribute that has different naming, you can do by adding hyper.remoteKey in the user info box with the value you want to map.

Custom remote key

Networking

You are free to use any networking library.

Supported iOS version

iOS 7 or above

Components

Sync wouldn't be possible without the help of this fully tested components:

  • DATAStack: Core Data stack and thread safe saving

  • DATAFilter: Helps you purge deleted objects, internally we use it to diff inserts, updates and deletes. Also it's used for uniquing Core Data does this based on objectIDs, DATAFilter uses your remote keys (such as id) for this

  • NSManagedObject-HYPPropertyMapper: Maps JSON fields with their Core Data counterparts, it does most of it's job using the paradigm "convention over configuration"

Author

Hyper Interkativ AS, [email protected], @hyperoslo

License

Sync is available under the MIT license. See the LICENSE file for more info.

sync's People

Contributors

3lvis avatar jgorset avatar kostiakoval avatar nselvis avatar ramongilabert avatar raven avatar zenangst avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.