Assets | ||
Config | ||
script | ||
SugarRecord | ||
SugarRecord.xcodeproj | ||
SugarRecordTests | ||
Wiki | ||
.cocoadocs.yml | ||
.gitignore | ||
.travis.yml | ||
Cartfile | ||
Cartfile.private | ||
Cartfile.resolved | ||
CHANGELOG.md | ||
CONDUCT.md | ||
CONTRIBUTING.md | ||
Gemfile | ||
Gemfile.lock | ||
LICENSE | ||
Makefile | ||
README.md | ||
SugarRecord.podspec |
SugarRecord
If you want to receive updates about the status of SugarRecord, you can subscribe to our mailing list here
What is SugarRecord?
SugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData/Realm/... 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 @pepibumur under SwiftReactive. You can reach me at pepibumur@gmail.com for help or whatever you need to commend about the library.
Features
- Swift 2.1 compatible (XCode 7.1).
- Fully rewritten from the version 1.0.
- Reactive API (using ReactiveCocoa).
- Protocols based design.
- For beginners and advanced users
- Fully customizable. Build your own stack!
- Friendly syntax (fluent)
- Away from Singleton patterns! No shared states 🎉
- 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 line `pod '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
Carthage
- Install Carthage on your computer using
brew install carthage
- Edit your
Cartfile
file adding the following linegithub 'swiftreactive/sugarrecord'
- Update and build frameworks with
carthage update
- Add generated frameworks to your app main target following the steps here
- Link your target with CoreData library (from Build Phases)
Notes
- The CocoaPods integration doesn't support Realm yet. Use Realm instead.
- Carthage integration includes both, CoreData and Carthage. We're planning to separate it in multiple frameworks. Task
- SugarRecord 2.0 is not compatible with the 1.x interface. If you were using that version you'll have to update your project to support this version.
Reference
You can check generated SugarRecord documentation here generated automatically with CocoaDocs
How to use
Creating your Storage
A storage represents your database, Realm, or CoreData. The first step to start using SugarRecord is initializing the storage. SugarRecord provides two default storages, one for CoreData, CoreDataDefaultStorage
and another one for Realm, RealmDefaultStorage
.
// Initializing CoreDataDefaultStorage
func coreDataStorage() -> CoreDataDefaultStorage {
let store = CoreData.Store.Named("db")
let bundle = NSBundle(forClass: CoreDataDefaultStorageTests.classForCoder())
let model = CoreData.ObjectModel.Merged([bundle])
let defaultStorage = try! CoreDataDefaultStorage(store: store, model: model)
return defaultStorage
}
// Initializing RealmDefaultStorage
func realmStorage() -> RealmDefaultStorage {
return RealmDefaultStorage()
}
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
, in case of Realm a wrapper around Realm
. 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 property
saveContext
of the storage is a computed property so every time you call it you get a new fresh context to be used. - MemoryContext: Use this context when you want to do some tests and you don't want your changes to be persisted.
Fetching data
Once you know that the context is the point to access the storage let's see how we can request objects since SugarRecord also provides a fluent interface to make things easier:
- Use the context
request()
method passing the type of object you want to fetch. - Specify filters and sort descriptors for that request. We'll expand this in the future to include more request features Realm/CoreData related.
- Once you have your request, just use the fetch method which will return a
Result<[Value], Error>
object that wraps both your result or an error in case of something went wrong with the request. You can unwrap the value withresult.value!
.
let pedros: [Person] = db.mainContext.request(Person.self).filteredWith("name", equalTo: "Pedro").fetch().value!
let tasks: [Task] = db.mainContext.request(Task.self).fetch().value!
let citiesByName: [City] = db.mainContext.request(City.self).sortedWith("name", ascending: true).fetch().value!
let predicate: NSPredicate = NSPredicate(format: "id == %@", "AAAA")
let john: User? = db.mainContext.request(User.self).filteredWith(predicate: predicate).fetch().value!.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.
db?.operation({ (context, save) -> Void in
// Do your operations here
save()
}, completed: {
// Everything was completed. :tada:
})
Inserting a model
You can use the insert()
method of context that needs the type of object you want to insert:
db?.operation({ (context, save) -> Void in
let newTask: Track = context.insert().value!
newTask.name = "Make CoreData easier!"
save()
}, completed: {
// Everything was completed. :tada:
})
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:
db?.operation({ (context, save) -> Void in
guard let john = db.mainContext.request(User.self).filteredWith("id", equalTo: "1234").fetch().value!.first else { return }
context.remove([john])
save()
}, completed: {
// Everything was completed. :tada:
})
> 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/Realm easier, funnier, and enjoyable is welcome! 🎉
Contributing
Trello board ✅
We ❤️ love transparency and decided to make the Trello board that use for organization public. You can access it here. You can check there the upcoming features and bugs pending to be fixed. You can also contribute proposing yours.
Support
If you want to communicate any issue, suggestion or even make a contribution, you have to keep in mind the flow bellow:
- If you need help, ask your doubt in Stack Overflow using the tag 'sugarrecord'
- If you want to ask something in general, use Stack Overflow too.
- Open an issue either when you have an error to report or a feature request.
- If you want to contribute, submit a pull request, and remember the rules to follow related with the code style, testing, ...
Contribution
- You'll find more details about contribution with SugarRecord in contribution
Resources
License
The MIT License (MIT)
Copyright (c) <2014> <Pedro Piñera>
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.
Who uses SugarRecord?
Are you using SugarRecord? Let us know, and we'll list you here. We ❤️ to hear about companies, apps that are using us with CoreData.