Core Data with ReactiveCocoa

Overview

ReactiveCoreData

ReactiveCoreData (RCD) is an attempt to bring Core Data into the ReactiveCocoa (RAC) world.

Currently has several files with the source code, Specta specs and a demo application for the Mac.

To use, copy the source files from ReactiveCoreData folder to your project. You should also have ReactiveCocoa in your project, of course.

Code from the Mac example:

// This part refetches data for the table and puts it into filteredParents
// It either fetches all Parents or filters by name, if there's something in the search field
// It will also refetch, if objectsChanged send a next
RAC(self.filteredParents) = [[[[Parent findAll]
    where:@"name" contains:filterText options:@"cd"]
    sortBy:@"name"]
    fetchWithTrigger:objectsChanged];

Another example of background processing:

[[[[triggerSignal 
    performInBackgroundContext:^(NSManagedObjectContext *context) {
        [Parent insert];
    }]
    saveContext]
    deliverOn:RACScheduler.mainThreadScheduler]
    subscribeNext:^(id _) {
        // Update UI
    }];
    
// We can also react to main context's merge notifications to update the UI
[mainContext.rcd_merged 
    subscribeNext:^(NSNotification *note){
        // Update UI
    }];

See the test Specs for some crude usage examples.

Also checkout the demo application in the project. It shows a simple table-view with Core Data backed storage using ReactiveCoreData and ReactiveCocoa for connecting things.

The headers also provide documentation for the various methods.

It's not feature-complete and more could be done but will be added based on actual usage and your contributions.

That being said, it should work both with shoebox and document-based applications, where there are many object contexts.

Done:

  • Start signals that represent and modify NSFetchRequests (findAll, findOne) from NSManagedObject.
  • -[RACSignal where:args:] method that sets a predicate with given format that can have signals as its arguments. This brings execution of NSFetchRequests into the Reactive domain. As any signal to predicate changes, the query is modified and sent next — to be fetched, for example.
  • A couple of signal-aware convenience methods for common predicate cases, like for CONTAINS predicate and for equal
  • [RACSignal limit:] that accepts either a value or a signal.
  • [RACSignal sortBy:] that accepts either a "key" string, or a "-key" (for descending sort), or a sort descriptor, or an array of sort descriptors, or a signal with any of these
  • fetch and count methods on RACSignal to execute the NSFetchRequest that's passed to them in the current NSManagedObjectContext and send the resulting array (or count) as "next".
  • fetchWithTrigger: which will rerun the fetch if the trigger fires any value.
  • fetching objectID and converting objectIDs to objects in current context
  • Running in background contexts
  • Saving of non-main contexts merges them into the main context.
  • Signal that's fired when a context is saved (wraps NSManagedObjectContextDidSaveNotification).
  • Signal that's fired after a merge.
  • Support not only for shoebox applications (with one main managed object context) but also document-based applications where you have a separate context for each document.
Comments
  • Generate compound predicate for multiple where clauses

    Generate compound predicate for multiple where clauses

    It would be useful to be able to do the following:

    [[[[MyEntity findAll] where:@"name" equals:@"joe"] where:@"cars.@count" equals:@(1)] fetch]

    and have this generate a compound predicate for the fetch request. I know you can accomplish the same thing without this today, but I find this a bit more readable. I'll have a pull request shortly.

    opened by jhosteny 1
  • Optimisations for swift

    Optimisations for swift

    I've changed the "where"-functions in RACSignal+ReactiveCoreData to avoid overlap with the swift keyword "where".

    In NSManagedObject+ReactiveCoreData i changed the way the entity name is build, with the StringFromClass()-methode swift will get a classname like "AppName.EntityName" which wont work for object creation.

    opened by iLem0n 0
  • method names will conflict potentially

    method names will conflict potentially

    Non-prefixed methods (e.g.: + (RACSignal *)findAll) will conflict with other libraries (e.g. MagicalRecord)

    So, all methods should be renamed to prefixed version.

    old

    + (RACSignal *)findAll;
    

    new

    + (RACSignal *)rac_findAll;
    

    FYI: "Namespacing - NSHipster" http://nshipster.com/namespacing/

    opened by AknEp 0
  • Examples of how to use/replace NSFetchedResultsController with RCD

    Examples of how to use/replace NSFetchedResultsController with RCD

    Lots of Core Data applications will use NSFetchedResultsController to manage their UITableViews and UICollectionViews.

    It provides a good level of control through its delegate protocol, and lets the user update tables relatively easily with some boilerplate code supplied by the Apple documentation.

    Where does RCD come in on this matter?

    opened by fatuhoku 6
  • Miscellaneous cleanup

    Miscellaneous cleanup

    Hi!

    First of all, I wanna say thanks for creating and sharing this cool stuff, I'm looking forward to using it in my projects. And I feel like there are a couple things that could be done to make it even better :)

    Here are the points I wanna ask you about:

    • Wouldn't it be better to use parent-child context hierarchy for background operations? I believe that this newer way is generally more preferable and convenient than doing merge with didSave notifications, because it doesn't require any additional setup. rcd_merged signal can be replaced with a signal for NSManagedObjectContextObjectsDidChangeNotification to serve the similar purpose.
    • This point kind of follows up the previous one. It feels like the methods + (NSManagedObjectContext *)context and + (NSManagedObjectContext *)currentContext in the NSManagedObjectContext (ReactiveCoreData) category are a bit confusing... Perhaps we could just eliminate the former one?
    • The methods - (RACSignal *)findAll: and - (RACSignal *)findOne: in RACSignal (ReactiveCoreData) category ignore the receiver completely and just return new signals... I guess they should be class methods instead?
    • Would it make sense to move methods + (instancetype)insert and + (instancetype)insert: from NSManagedObject (ReactiveCoreData) to some other place or maybe remove them completely? They seem like they have nothing to do with ReactiveCocoa stuff :)

    Let me know what do you think about this. If you are okay with these points, I'm willing to contribute :)

    opened by nickynick 12
Owner
Jacob Gorban
Founder of Apparent Software. Cocoa developer
Jacob Gorban
Set of useful extensions for ReactiveSwift & ReactiveCocoa

ACKReactiveExtensions ACKReactiveExtensions is set of useful extensions for ReactiveCocoa you could use in your apps. Currently we have extensions for

Ackee 17 Aug 12, 2022
ReactiveCocoa wrapper for CLLocationManager.

ReactiveSwift wrapper to observe location Our wrapper automatically asks for permission. It determines what kind of permission your app requires by ch

Ackee 24 Sep 6, 2022
RxSwift extensions for Core Data

RxCoreData Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Xcode 9.0 Swift 4.0

RxSwift Community 164 Oct 14, 2022
Unidirectional Data Flow in Swift - Inspired by Redux

ReSwift Supported Swift Versions: Swift 4.2, 5.x For Swift 3.2 or 4.0 Support use Release 5.0.0 or earlier. For Swift 2.2 Support use Release 2.0.0 or

null 7.3k Dec 25, 2022
🔄 Unidirectional data flow in Swift.

Reactor Reactor is a framework for making more reactive applications inspired by Elm, Redux, and recent work on ReSwift. It's small and simple (just o

Reactor 175 Jul 9, 2022
Two-way data binding framework for iOS. Only one API to learn.

BindKit A simple to use two-way data binding framework for iOS. Only one API to learn. Supports Objective-C, Swift 5, Xcode 10.2, iOS 8 and above. Shi

Electric Bolt 13 May 25, 2022
This Repository holds learning data on Combine Framework

Combine Framework List of Topics Welcome, every section in this repo contains a collection of exercises demonstrating combine's utilization as well as

Julio Ismael Robles 2 Mar 17, 2022
Unidirectional Data Flow in Swift - Inspired by Redux

ReSwift Supported Swift Versions: Swift 4.2, 5.x For Swift 3.2 or 4.0 Support use Release 5.0.0 or earlier. For Swift 2.2 Support use Release 2.0.0 or

null 7.3k Jan 3, 2023
Binding - Data binding framework (view model binding on MVVM) written using propertyWrapper and resultBuilder

Binding Data binding framework (view model binding on MVVM) written using @prope

Sugeng Wibowo 4 Mar 23, 2022
A super simple library for state management with unidirectional data flow.

OneWay ?? OneWay is still experimental. As such, expect things to break and change in the coming months. OneWay is a super simple library for state ma

SeungYeop Yeom 41 Dec 20, 2022
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
An array class implemented in Swift that can be observed using ReactiveCocoa's Signals

ReactiveArray An array class implemented in Swift that can be observed using ReactiveCocoa's Signals. Installation Carthage Add the following to your

Wolox 53 Jan 29, 2022
Set of useful extensions for ReactiveSwift & ReactiveCocoa

ACKReactiveExtensions ACKReactiveExtensions is set of useful extensions for ReactiveCocoa you could use in your apps. Currently we have extensions for

Ackee 17 Aug 12, 2022
ReactiveCocoa wrapper for CLLocationManager.

ReactiveSwift wrapper to observe location Our wrapper automatically asks for permission. It determines what kind of permission your app requires by ch

Ackee 24 Sep 6, 2022
A Swift implementation of a Flickr-search application that uses MVVM and ReactiveCocoa

ReactiveCocoa, Swift and MVVM This application is a Swift-port of an MVVM / ReactiveCocoa example application I wrote a few months ago. Instructions T

Colin Eberhardt 377 Sep 26, 2022
iOS client for Giphy written in Swift with ReactiveCocoa

Giraffe iOS client for Giphy written in Swift with ReactiveCocoa Requirements iOS 10.0 Beta Xcode 8 Beta 2 Swift 2.3 Carthage Why S

Yevhen Dubinin 43 Feb 10, 2022
IOS-App4 - Core Data add new data and update data

iOS-App4 Core Data add new data and update data.

null 0 Jan 8, 2022
Notes App using Core Data for persisting the data ✨

Notes Notes app where we can save and manage our daily notes. App usage The app allow us to add new notes We can delete our existing notes We can edit

Chris 0 Nov 13, 2021
Task App for Swift that Persist Data with Core Data (iOS)

Originally by: Michael Crump Updates for Xcode 10 with Swift 4.2 by David Phillip Oster

Michael Crump 16 Aug 3, 2021