Awesome
<center></center>
What is SugarRecord?
SugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData in a much easier way. Thanks to SugarRecord you'll be able to use CoreData with just a few lines of code: Just choose your stack and start playing with your data.
The library is maintained by @carambalabs. You can reach me at pepibumur@gmail.com for help or whatever you need to commend about the library.
Features
- Swift 3.0 compatible (Xcode 8.0).
- Protocols based design.
- For beginners and advanced users
- Fully customizable. Build your own stack!
- Friendly syntax (fluent)
- Away from Singleton patterns! No shared states :tada:
- Compatible with OSX/iOS/watchOS/tvOS
- Fully tested (thanks Nimble and Quick)
- Actively supported
Setup
CocoaPods
- Install CocoaPods. You can do it with
gem install cocoapods
- Edit your
Podfile
file and add the following linepod 'SugarRecord'
- Update your pods with the command
pod install
- Open the project from the generated workspace (
.xcworkspace
file).
Note: You can also test the last commits by specifying it directly in the Podfile line
Available specs Choose the right one depending ton the configuration you need for you app.
pod "SugarRecord/CoreData"
pod "SugarRecord/CoreData+iCloud"
Carthage
- Install Carthage. You can do it with
brew install carthage
. - Edit your
Cartfile
file and add the following line `github "carambalabs/sugarrecord". - Execute
carthage update
- Add the frameworks to your project as explained on the Carthage repository.
Reference
You can check generated SugarRecord documentation here generated automatically with CocoaDocs
How to use
Creating your Storage
A storage represents your database. The first step to start using SugarRecord is initializing the storage. SugarRecord provides a default storages, CoreDataDefaultStorage
.
// Initializing CoreDataDefaultStorage
func coreDataStorage() -> CoreDataDefaultStorage {
let store = CoreDataStore.named("db")
let bundle = Bundle(for: self.classForCoder)
let model = CoreDataObjectModel.merged([bundle])
let defaultStorage = try! CoreDataDefaultStorage(store: store, model: model)
return defaultStorage
}
Creating an iCloud Storage
SugarRecord supports the integration of CoreData with iCloud. It's very easy to setup since it's implemented in its own storage that you can use from your app, CoreDataiCloudStorage
:
// Initializes the CoreDataiCloudStorage
func icloudStorage() -> CoreDataiCloudStorage {
let bundle = Bundle(for: self.classForCoder)
let model = CoreDataObjectModel.merged([bundle])
let icloudConfig = CoreDataiCloudConfig(ubiquitousContentName: "MyDb", ubiquitousContentURL: "Path/", ubiquitousContainerIdentifier: "com.company.MyApp.anothercontainer")
let icloudStorage = try! CoreDataiCloudStorage(model: model, iCloud: icloudConfig)
return icloudStorage
}
Contexts
Storages offer multiple kind of contexts that are the entry points to the database. For curious developers, in case of CoreData a context is a wrapper around NSManagedObjectContext
. The available contexts are:
- MainContext: Use it for main thread operations, for example fetches whose data will be presented in the UI.
- SaveContext: Use this context for background operations. The context is initialized when the storage instance is created. That context is used for storage operations.
- MemoryContext: Use this context when you want to do some tests and you don't want your changes to be persisted.
Fetching data
let pedros: [Person] = try! db.fetch(FetchRequest<Person>().filtered(with: "name", equalTo: "Pedro"))
let tasks: [Task] = try! db.fetch(FetchRequest<Task>())
let citiesByName: [City] = try! db.fetch(FetchRequest<City>().sorted(with: "name", ascending: true))
let predicate: NSPredicate = NSPredicate(format: "id == %@", "AAAA")
let john: User? = try! db.fetch(FetchRequest<User>().filtered(with: predicate)).first
Remove/Insert/Update operations
Although Context
s offer insertion
and deletion
methods that you can use it directly SugarRecords aims at using the operation
method method provided by the storage for operations that imply modifications of the database models:
- Context: You can use it for fetching, inserting, deleting. Whatever you need to do with your data.
- Save: All the changes you apply to that context are in a memory state unless you call the
save()
method. That method will persist the changes to your store and propagate them across all the available contexts.
do {
db.operation { (context, save) throws in
// Do your operations here
try save()
}
} catch {
// There was an error in the operation
}
New model
You can use the context new()
method to initialize a model without inserting it in the context:
do {
db.operation { (context, save) throws in
let newTask: Track = try context.new()
newTask.name = "Make CoreData easier!"
try context.insert(newTask)
try save()
}
} catch {
// There was an error in the operation
}
In order to insert the model into the context you use the insert() method.
Creating a model
You can use the create()
for initializing and inserting in the context in the same operation:
do {
db.operation { (context, save) throws -> Void in
let newTask: Track = try! context.create()
newTask.name = "Make CoreData easier!"
save()
}
}
catch {
// There was an error in the operation
}
Delete a model
In a similar way you can use the remove()
method from the context passing the objects you want to remove from the database:
do {
db.operation { (context, save) throws in
let john: User? = try context.request(User.self).filteredWith("id", equalTo: "1234").fetch().first
if let john = john {
try context.remove([john])
try save()
}
}
} catch {
// There was an error in the operation
}
<br>
> This is the first approach of SugarRecord for the interface. We'll improve it with the feedback you can report and according to the use of the framework. Do not hesitate to reach us with your proposals. Everything that has to be with making the use of CoreData easier, funnier, and enjoyable is welcome! :tada:
RequestObservable
SugarRecord provides a component, RequestObservable
that allows observing changes in the DataBase. It uses NSFetchedResultsController
under the hood.
Observing
class Presenter {
var observable: RequestObservable<Track>!
func setup() {
let request: FetchRequest<Track> = FetchRequest<Track>().filtered(with: "artist", equalTo: "pedro")
self.observable = storage.instance.observable(request)
self.observable.observe { changes in
case .Initial(let objects):
print("\(objects.count) objects in the database")
case .Update(let deletions, let insertions, let modifications):
print("\(deletions.count) deleted | \(insertions.count) inserted | \(modifications.count) modified")
case .Error(let error):
print("Something went wrong")
}
}
}
Retain: RequestObservable must be retained during the observation lifecycle. When the
RequestObservable
instance gets released from memory it stops observing changes from your storage.
NOTE: This was renamed from Observable -> RequestObservable so we are no longer stomping on the RxSwift Observable namespace.
:warning: RequestObservable
is only available for CoreData + OSX since MacOS 10.12
Resources
Contributors
<img alt="glebo" src="https://avatars3.githubusercontent.com/u/3298239?v=4&s=117" width="117"><img alt="sushichop" src="https://avatars3.githubusercontent.com/u/5669641?v=4&s=117" width="117"><img alt="foxling" src="https://avatars3.githubusercontent.com/u/506125?v=4&s=117" width="117"><img alt="ZevEisenberg" src="https://avatars2.githubusercontent.com/u/464574?v=4&s=117" width="117"><img alt="konyu" src="https://avatars2.githubusercontent.com/u/1217706?v=4&s=117" width="117">
<img alt="yuuki1224" src="https://avatars2.githubusercontent.com/u/1756640?v=4&s=117" width="117"><img alt="YTN01" src="https://avatars3.githubusercontent.com/u/5421955?v=4&s=117" width="117"><img alt="gitter-badger" src="https://avatars2.githubusercontent.com/u/8518239?v=4&s=117" width="117"><img alt="sergigracia" src="https://avatars3.githubusercontent.com/u/1061658?v=4&s=117" width="117"><img alt="adityatrivedi" src="https://avatars0.githubusercontent.com/u/1791760?v=4&s=117" width="117">
<img alt="Adlai-Holler" src="https://avatars2.githubusercontent.com/u/2466893?v=4&s=117" width="117"><img alt="akshaynhegde" src="https://avatars0.githubusercontent.com/u/3814615?v=4&s=117" width="117"><img alt="goingreen" src="https://avatars2.githubusercontent.com/u/16046834?v=4&s=117" width="117"><img alt="startupthekid" src="https://avatars3.githubusercontent.com/u/3139429?v=4&s=117" width="117"><img alt="ctotheameron" src="https://avatars0.githubusercontent.com/u/3002968?v=4&s=117" width="117">
<img alt="davidahouse" src="https://avatars2.githubusercontent.com/u/140127?v=4&s=117" width="117"><img alt="A8-Moke" src="https://avatars2.githubusercontent.com/u/12935988?v=4&s=117" width="117"><img alt="Arasthel" src="https://avatars1.githubusercontent.com/u/480955?v=4&s=117" width="117"><img alt="LuizZak" src="https://avatars3.githubusercontent.com/u/6502879?v=4&s=117" width="117"><img alt="literator" src="https://avatars1.githubusercontent.com/u/242131?v=4&s=117" width="117">
<img alt="madeinqc" src="https://avatars0.githubusercontent.com/u/7191124?v=4&s=117" width="117"><img alt="kolisko" src="https://avatars0.githubusercontent.com/u/460056?v=4&s=117" width="117"><img alt="dukemike" src="https://avatars3.githubusercontent.com/u/11562781?v=4&s=117" width="117"><img alt="rafalwojcik" src="https://avatars3.githubusercontent.com/u/512353?v=4&s=117" width="117"><img alt="thad" src="https://avatars1.githubusercontent.com/u/139789?v=4&s=117" width="117">
<img alt="chrispix" src="https://avatars0.githubusercontent.com/u/190962?v=4&s=117" width="117"><img alt="ReadmeCritic" src="https://avatars3.githubusercontent.com/u/15367484?v=4&s=117" width="117"><img alt="avielg" src="https://avatars3.githubusercontent.com/u/5012557?v=4&s=117" width="117"><img alt="rdougan" src="https://avatars3.githubusercontent.com/u/28582?v=4&s=117" width="117"><img alt="grangej" src="https://avatars0.githubusercontent.com/u/604788?v=4&s=117" width="117">
<img alt="fjbelchi" src="https://avatars2.githubusercontent.com/u/626713?v=4&s=117" width="117"><img alt="dcvz" src="https://avatars0.githubusercontent.com/u/2475932?v=4&s=117" width="117"><img alt="pepibumur" src="https://avatars3.githubusercontent.com/u/663605?v=4&s=117" width="117">
About
<img src="https://github.com/carambalabs/Foundation/blob/master/ASSETS/logo-salmon.png?raw=true" width="200" />This project is funded and maintained by Caramba. We 💛 open source software!
Check out our other open source projects, read our blog or say :wave: on twitter @carambalabs.
Contribute
Contributions are welcome :metal: We encourage developers like you to help us improve the projects we've shared with the community. Please see the Contributing Guide and the Code of Conduct.
License
The MIT License (MIT)
Copyright (c) 2017 Caramba
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.