Eliminate your Core Data boilerplate code

Related tags

Core Data ssdatakit
Overview

SSDataKit

There is a lot of boilerplate code required to write a Core Data application. This is annoying. In pretty much everything I've written since Core Data came to iOS, I have used the following class.

What's Included

SSManagedObject

  • Manages main context, persistent store, etc
  • Accessing entity descriptions
  • Reflection
  • Easy creating and deleting

SSRemoteManagedObject

  • Easily find or create objects by a remote ID
  • Unpack NSDictionary's into your Core Data object's attributes

Example

This is very simple example of how to use SSRemoteManagedObject.

Post.m

- (void)unpackDictionary:(NSDictionary *)dictionary {
  [super unpackDictionary:dictionary];
  self.title = dictionary[@"title"];
}

Now you can create and find posts easily.

Post *post = [Post objectWithDictionary:@{@"id": @(1), @"title": @"Hello World"}];
Post *anotherPost = [Post objectWithRemoteID:@(1)];
NSLog(@"Equal: %i", [post isEqual:anotherPost]); // Equal: 1

For a more complete example, see CheddarKit which is used in Cheddar for iOS and Cheddar for Mac.

Comments
  • Changes in privateQueueContext are not automatically propagated to mainQueueContext

    Changes in privateQueueContext are not automatically propagated to mainQueueContext

    I'm using SSDataKit along with AFIncrementalStore and discover interesting behavior.

    Seems like when NSFetchedResultController performing fetch, implicitly uses topmost PSC of parent/child MOC chain. I swizzle -executeRequest:withContext:error: method of NSPersistentStoreCoordinator, and it is called with privateQueueContext as context parameter, while NSFetchedResultController is initialized with mainQueueContext.

    AFIncrementalStore can intercept fetch requests and change context before (or after in async manner) returning any results back to caller. In my case NSIncrementalStore method -executeRequest:withContext:error: is also called with privateQueueContext as parameter. Then AFIncrementalStore getting data from remote server and update privateQueueContext asynchronously. But this changes never automatically pulled by mainQueueContext.

    Now I just override -mainQueueContext method to not use private moc as parent, but looking for more appropriate solution to pull changes back to main moc in case of async update in private moc.

    opened by ploddi 9
  • doesn't work with OCTests

    doesn't work with OCTests

    Hi, my app works fine with SSDataKit but when I try to run some Unit Tests, I get the exception

    An NSManagedObject of class 'XXXX' must have a valid NSEntityDescription.
    

    when instantiating a new managed object. My model definition is included in the test target.

    any idea what is wrong?

    cheers

    opened by JanC 6
  • Adding SSManagedCollectionViewController

    Adding SSManagedCollectionViewController

    I'm using SSDataKit for a little project of mine, but needed to use UICollectionViewCollection instead of UITableViewController - so I decided to implement it myself.

    Everything works. Very similar API to UITableView.

    opened by rdougan 6
  • Issue with cocoapods

    Issue with cocoapods

    So here's how to reproduce the bug.

    1. In my podfile, I have ~> 0.3.0.
    2. If I run pod update nothing gets updated. I still have the old code.
    3. Tried deleting the Pod-SSDataKit directory and re-run pod install and pod update
    4. Still gets the same, old version.

    I have cocoapods 0.29 BTW. Tried sudo gem install/update cocoapods without any luck.

    Should I just do git pull origin master in the pod directory ?

    Thanks a lot.

    opened by nitriques 5
  • -parseDate: not parsing valid ISO 8601 format from Rails

    -parseDate: not parsing valid ISO 8601 format from Rails

    Just ran into this issue and wanted to get an issue started, PR coming soon.

    Method: DateTime.now.utc.to_s Format: 2014-09-11T20:58:54+00:00

    An offset of zero, in addition to having the special representation "Z", can also be stated numerically as "+00:00", "+0000", or "+00".

    Hits this condition in SSRemoteManagedObject and method is like

    opened by mwhuss 4
  • Ensure that remoteID is always a number value

    Ensure that remoteID is always a number value

    I work with a lot of (subpar) APIs. Some are nicer than others, but one I'm working with in particular sends all JSON over as string values, no integers. With remoteIDs, this causes a crash when the value for @"id" is a string and not an int. Easy enough to just cast it.

    opened by eliperkins 4
  • Bug in parseDate

    Bug in parseDate

    I referenced the parser in SSToolkit and it looks like there's a typo.

    There are no tests, but without this change, my dates (from Rails to_json) aren't able to be parsed. After the change, everything works fine.

    opened by devinfoley 4
  • UIKit on cocoa apps

    UIKit on cocoa apps

    I can't make it work in my cocoa app because UIKit library, errors located at: SSFilteredResultsFilter.m and SSFilterableFetchedResultsController.h

    How should I handle this?

    .../SSDataKit/SSFilterableFetchedResultsController.h:10:9: fatal error: 'UIKit/UIKit.h' file not found
    #import <UIKit/UIKit.h>
            ^
    1 error generated.
    
    opened by NicosKaralis 3
  • Clean up WAL files during reset

    Clean up WAL files during reset

    See issue #29 for why I made this change. In short, this patch just deletes the -wal and -shm files created by SQLite WAL mode. (Enabled by default on iOS7.)

    I couldn't easily include my tests for this, because of the the way the SSDataKit tests are structured. But I did test this patch for both WAL mode and DELETE mode. Unfortunately, I was not able to test on older versions of iOS, but it should be just fine. As you can see, there isn't much too it.

    opened by davidogren 3
  • Refactor the fetched results controller and collection interaction.

    Refactor the fetched results controller and collection interaction.

    I made the following changes:

    • I think that performing two sets of batch updates in controllerDidChangeContent: is dangerous. At the end of the first update, the collection view could ask the data source for data that is now outdated which will cause a crash. I folded both update blocks into a single pass. #24
    • I don't like the idea of using the mutable collections directly inside the change block since no garuntee is made as to when it will run. So, I copy the pending changes into stack variables and immeditely reset the mutable collections. This prevents the collections from being mutated in multiple quick passes through the fetched results controller methods. #24
    • Added ability to disable change animations. #25
    • Begin translating index paths from fetched index paths into view index paths. #24.
    • Translate group indexes from fetched indexes to view indexes so that the appropriate groups are updated. #24
    opened by calebd 3
  • ISO8601

    ISO8601

    This addresses #35. Since you recommended using the ISO8601 lib for date parsing it seemed appropriate that SSDataKit use it as well. This PR adds Cocoapods to the test project and then updated the -parseDate: method to use the ISO8601 lib for when the input was a string.

    ~~NOTE: The 2 existing ISO8601 tests now fail, but my added test now passes.~~ Fixed.

    opened by mwhuss 2
  • Fix for possible infinite loop for persistent store in bad state

    Fix for possible infinite loop for persistent store in bad state

    There exists an issue that if the NSPersistentStroeCoordinator failed to add a persistent store, then the coordinator was in a bad state. In this case, the code just continued to use that coordinator even though it was in a bad state, and then performing fetches or saves on the coordinator would cause a crash.

    The fix only sets the static coordinator upon successfully adding a persistentStore, otherwise leaving it as nil. The second part of this is making it so that if the coordinator is nil, we still allow the code to run to make a new one to it can try and make it again.

    opened by zbigy 0
  • WAL and SHM files not cleaned up during reset

    WAL and SHM files not cleaned up during reset

    While running unit tests on my SSDataKit enabled project I was hitting some SQLite 522 'not an error' errors. Those unit tests were on iOS7 (which has WAL enabled by default) and the tests frequently run SSManagedObject +resetPersistentStore during setup.

    Some Googling and StackExchange searching led me to a theory that this might be caused by WAL mode, and particularly by the inconsistent state caused by having a new sqlite database file created, but still having an old WAL journal. As part of exploring that theory, I modified SSDataKit to clean up those -shm -wal files used by WAL when SSDataKit resets the persistent store or is cleaning up after a failed lightweight migration.

    That theory didn't pan out, but I wanted to offer those changes back to you regardless. (I'll send a pull request right after I submit this issue.) Feel free to reject the request if you know that SQLite will always do the right thing with just the sqlite file removed. (Which, in theory, it should be able to do.)

    opened by davidogren 4
  • SSDataKit 1.0

    SSDataKit 1.0

    I want to make several larger changes to SSDataKit. Here's the list:

    • [ ] Rename to DataKit or SAMDataKit
    • [x] Make the primary key (currently remoteID) configurable #20
    • [ ] Automatic unpacking by default (can override to customize)
    • [ ] Figure out a better nested context solution #11
    • [ ] Proper demo #1
    • [x] Full documentation
    • [ ] Test SSManagedObject
    • [ ] Test SSRemoteManagedObject
    • [ ] Test fetched results controller craziness
    opened by soffes 1
  • Refactor SSFilterableFetchedResultsController filter updates.

    Refactor SSFilterableFetchedResultsController filter updates.

    Currently, whenever there's an change in the filter in a SSFilterableFetchedResultsController, the entire table is deleted and the new results are filtered.

    With this change, only objects that do not pass the new filter are deleted, objects that pass both filters are moved if necessary, and objects that only pass the new filter (but not the old one) are inserted.

    Thanks for all your hard work on this project! It seriously helped my understanding and use of CoreData.

    opened by maxmeyers 4
Owner
Sam Soffes
Living that #vanlife
Sam Soffes
100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer

DATAStack helps you to alleviate the Core Data boilerplate. Now you can go to your AppDelegate remove all the Core Data related code and replace it wi

Nes 216 Jan 3, 2023
JSON to Core Data and back. Swift Core Data Sync.

Notice: Sync was supported from it's creation back in 2014 until March 2021 Moving forward I won't be able to support this project since I'm no longer

Nes 2.5k Dec 31, 2022
Core Data Generator (CDG for short) is a framework for generation (using Sourcery) of Core Data entities from plain structs/classes/enums.

Core Data Generator Introduction Features Supported platforms Installation CDG Setup RepositoryType ModelType DataStoreVersion MigrationPolicy Basic U

Lotusflare 18 Sep 19, 2022
Core Data code generation

mogenerator Visit the project's pretty homepage. Here's mogenerator's elevator pitch: mogenerator is a command-line tool that, given an .xcdatamodel f

Wolf Rentzsch 3k Dec 30, 2022
Super awesome Swift minion for Core Data (iOS, macOS, tvOS)

⚠️ Since this repository is going to be archived soon, I suggest migrating to NSPersistentContainer instead (available since iOS 10). For other conven

Marko Tadić 306 Sep 23, 2022
A powerful and elegant Core Data framework for Swift.

A powerful and elegant Core Data framework for Swift. Usage Beta version. New docs soon... Simple do that: let query = persistentContainer.viewContext

null 782 Nov 6, 2022
CloudCore is a framework that manages syncing between iCloud (CloudKit) and Core Data written on native Swift.

CloudCore CloudCore is a framework that manages syncing between iCloud (CloudKit) and Core Data written on native Swift. Features Leveraging NSPersist

deeje cooley 123 Dec 31, 2022
Unleashing the real power of Core Data with the elegance and safety of Swift

Unleashing the real power of Core Data with the elegance and safety of Swift Dependency managers Contact Swift 5.4: iOS 11+ / macOS 10.13+ / watchOS 4

John Estropia 3.7k Jan 9, 2023
JSQCoreDataKit - A swifter Core Data stack

JSQCoreDataKit A swifter Core Data stack About This library aims to do the following: Encode Core Data best practices, so you don't have to think "is

Jesse Squires 596 Dec 3, 2022
JustPersist is the easiest and safest way to do persistence on iOS with Core Data support out of the box.

JustPersist JustPersist is the easiest and safest way to do persistence on iOS with Core Data support out of the box. It also allows you to migrate to

Just Eat 167 Mar 13, 2022
QueryKit, a simple type-safe Core Data query language.

QueryKit QueryKit, a simple type-safe Core Data query language. Usage QuerySet<Person>(context, "Person")

QueryKit 1.5k Dec 20, 2022
HitList is a Swift App shows the implementation of Core Data.

HitList HitList is a Swift App shows the implementation of Core Data. It is the demo app of Ray Wenderlich's tech blog. For details please reference G

Kushal Shingote 2 Dec 9, 2022
A synchronization framework for Core Data.

Core Data Ensembles Author: Drew McCormack Created: 29th September, 2013 Last Updated: 15th February, 2017 Ensembles 2 is now available for purchase a

Drew McCormack 1.6k Dec 6, 2022
Super Awesome Easy Fetching for Core Data!

MagicalRecord In software engineering, the active record pattern is a design pattern found in software that stores its data in relational databases. I

Magical Panda Software 10.9k Dec 29, 2022
A type-safe, fluent Swift library for working with Core Data

Core Data Query Interface (CDQI) is a type-safe, fluent, intuitive library for working with Core Data in Swift. CDQI tremendously reduces the amount o

null 31 Oct 26, 2022
A feature-light wrapper around Core Data that simplifies common database operations.

Introduction Core Data Dandy is a feature-light wrapper around Core Data that simplifies common database operations. Feature summary Initializes and m

Fuzz Productions 33 May 11, 2022
The Big Nerd Ranch Core Data Stack

BNR Core Data Stack The BNR Core Data Stack is a small Swift framework that makes it both easier and safer to use Core Data. A better fetched results

Big Nerd Ranch 561 Jul 29, 2022
Example repo of working with Core Data in a SwiftUI application

CoreData in SwiftUI This repository serves many purpose. But mostly so I can experiment with Core Data in SwiftUI, and also so I can use it show my ap

Donny Wals 4 Jan 30, 2022
This project server as a demo for anyone who wishes to learn Core Data in Swift.

CoreDataDemo This project server as a demo for anyone who wishes to learn Core Data in Swift. The purpose of this project is to help someone new to Co

null 1 May 3, 2022