Skip to content

Repository files navigation

CoreDataStorage

VersionPlatformLicense

Comfort CoreData with Combine

Requirements

iOSVersionSwiftVersion

Usage & Example

Connect

// Example Struct
structMovie{letid:Stringlettitle:StringletreleaseDate:Dateletdesc:String}
// Struct connect Entity
extensionMovie:Entitable{
// Create toEntity Function
func toEntity(in context:NSManagedObjectContext)->MovieEntity{letentity:MovieEntity=.init(context: context)
entity.id = id
entity.title = title
entity.releaseDate = releaseDate
entity.desc = desc
return entity
}}
// Entity connect Struct
extensionMovieEntity:Objectable{
// Create toObject Function
publicfunc toObject()->someEntitable{returnMovie(id: id ??UUID().uuidString,
title: title ??"unknown",
releaseDate: releaseDate ??Date(),
desc: desc ??"")}}

CoreDataStorage

letcoreDataStorage=CoreDataStorage.shared(name:"Storage Name")
// CoreDataStorage Function
func create<O>(_ value:O)->AnyPublisher<O,Error>where O:Entitablefunc read<O: Entitable>(type:O.Type, predicate:NSPredicate?=nil, sortDescriptors:[NSSortDescriptor]?=nil)->AnyPublisher<[O],Error>func update<O: Entitable>(_ updateObject:O, predicate:NSPredicate, limit:Int?=nil)->AnyPublisher<[O],Error>func update<O: Entitable>(type:O.Type, updateValues:[String:Any], predicate:NSPredicate)->AnyPublisher<[O],Error>func delete<O: Entitable>(_ type:O.Type, predicate:NSPredicate, limit:Int?=nil)->AnyPublisher<[O],Error>func deleteAll<O: Entitable>(_ type:O.Type)->AnyPublisher<Bool,Error>

Create

// MARK: - Functions
func create<O>(_ value:O)->AnyPublisher<O,Error>where O:Entitable
// MARK: - Example
func createMovie(movie: Movie)->AnyPublisher<Movie,Error>{returnCoreDataStorage.shared(name:"MovieStorage").create(movie)}

Read

// MARK: - Functions
func read<O:Entitable>(type:O.Type, predicate:NSPredicate?=nil, sortDescriptors:[NSSortDescriptor]?=nil)->AnyPublisher<[O],Error>
// MARK: - Example
func readMovie(id:String)->AnyPublisher<[Movie],Error>{letpredicate=NSPredicate(format:"id = %@","\(id)")returnCoreDataStorage.shared(name:"MovieStorage").read(type:Movie.self, predicate: predicate)}func readAllMovie()->AnyPublisher<[Movie],Error>{returnCoreDataStorage.shared(name:"MovieStorage").read(type:Movie.self)}

Update

// MARK: - Functions
func update<O:Entitable>(_ updateObject:O, predicate:NSPredicate, limit:Int?=nil)->AnyPublisher<[O],Error>func update<O: Entitable>(type:O.Type, updateValues:[String:Any], predicate:NSPredicate)->AnyPublisher<[O],Error>
// MARK: - Example
func updateMovie(movie:Movie)->AnyPublisher<[Movie],Error>{letpredicate=NSPredicate(format:"id = %@","\(movie.id)")returnCoreDataStorage.shared(name:"MovieStorage").update(movie, predicate: predicate)}func updateMovie(id:String)->AnyPublisher<[Movie],Error>{letpredicate=NSPredicate(format:"id = %@","\(movie.id)")returnCoreDataStorage.shared(name:"MovieStorage").update(Movie.self, updateValues:[title:"unknown"], predicate: predicate)}

Delete

// MARK: - Functions
func delete<O:Entitable>(_ type:O.Type, predicate:NSPredicate, limit:Int?=nil)->AnyPublisher<[O],Error>func deleteAll<O: Entitable>(_ type:O.Type)->AnyPublisher<Bool,Error>
// MARK: - Example
func deleteMovie(id:String)->AnyPublisher<[Movie],Error>{letpredicate=NSPredicate(format:"id = %@","\(id)")returnCoreDataStorage.shared(name:"MovieStorage").delete(Movie.self, predicate: predicate)}func deleteAllMovies(movie:Movie)->AnyPublisher<Bool,Error>{returnCoreDataStorage.shared(name:"MovieStorage").deleteAll(Movie.self)}

Swift Package Manager

https://github.com/JHKits/CoreDataStorage

CocoaPods

CoreDataStorage is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod'CoreDataStorage'

Author

Jeonhui, l06094@gmail.com

License

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

About

Comfort CoreData with Combine

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages