Skip to content
This repository was archived by the owner on Nov 9, 2021. It is now read-only.

Repository files navigation

CoreDataStore

CI StatusVersionLicensePlatform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

CoreDataStore currently is not available through CocoaPods public repo. To install it, simply add the following line to your Podfile:

pod'CoreDataStore'

Example

Initialize store:

guardlet modelURL =CoreDataStore.defaultModelURL()else{return}guardlet storeURL =CoreDataStore.defaultStoreURL()else{return}letstore=CoreDataStore(
modelURL: modelURL,
storeType:.sqlite(
storeURL: storeURL,
fileProtection:.complete
))
store.initialize{(result)inswitch result {case.success:
// Now - use store
default:break}}

Extend some type (here User) to be stored by some store representation (for example, you have someCDUser CoreData entity):

structUser{varid=UUID().uuidString
varfirstName=UUID().uuidString
}extensionUser:CoreDataStoreRepresentable,StoreIdentifiable{typealiasRepresentationRequest=NSFetchRequest<CDUser>staticvaridentifierKey:String{"id"}staticfunc from(_ representation:CDUser, in context:NSManagedObjectContext)throws->User{.init(id: representation.id ??"", firstName: representation.firstName ??"")}func update(_ representation:CDUser, in context:NSManagedObjectContext)throws{
representation.id = id
representation.firstName = firstName
}}

Manage store data:

lettransaction= store.createTransaction()do{try transaction.run{ _, context inprint("Existed: \n\(try context.fetch(User.self))")try context.delete(User.self)print("After delete: \n\(try context.fetch(User.self))")try context.insert([User(),User(),User(id:"1", firstName:"Special")])print("After insert: \n\(try context.fetch(User.self))")letspecial=try context.fetch(User.self, byID:"1")print(String(describing: special))}
transaction.commit()}catch{
transaction.rollback()}

Author

Koshub

License

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

About

CoreData Store Wrapper with Transactions

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages