Comments (9)
Hi @andrewSeregin I had the same problem, and was wondering how to use Persitables with relationships, and I found solution (I'm not sure if it's the best one). This is how it works, so in theory Persistable protocol just transforms your custom structs to NSManaged objects right? Problem is that if you want to implement One To Many relationship, you cant write an Set of your custom objects to core data because core data are excepting subclass of NSManagedObject. So what I did is this:
public struct Parrent {
var id: String
var name: String
var kids: Set<Kid> = Set<Kid>()
init(id: String, name: String, kids: Set<Kid>) {
self.id = id
self.name = name
self.kids = kids
}
}
extension Parrent: Equatable {}
public func == (lhs: Parrent, rhs: Parrent) -> Bool {
return lhs.id == rhs.id
}
extension Parrent: Persistable {
public typealias T = NSManagedObject
public static var entityName: String {
return "Parrent"
}
public static var primaryAttributeName: String {
return "id"
}
public init(entity: T) {
self.id = entity.value(forKey: "id") as! String
self.name = entity.value(forKey: "neme") as! String
let kidsEntitySet = entity.value(forKey: "kids") as! Set<Kid.T>
self.kids = Set<Kid>(kidsEntitySet.map { kidEntity in
Kid(entity: kidEntity)
})
}
public func update(_ entity: T) {
entity.setValue(self.id, forKey: "id")
let etityArray: Array<Kid.T>! = self.kids.map { kid in
//he you have to use your own instance of managedObjectContext, this wont compile for you otherwise everithing should work
let emptyEntity = coreDataStack.managedObjectContext.rx.getOrCreateEntity(for: kid)
kid.update(emptyEntity)
return emptyEntity
}
entity.setValue(Set<Kid.T>(etityArray), forKey: "kids")
do {
try entity.managedObjectContext?.save()
} catch let e {
print(e)
}
}
}
extension Parrent: IdentifiableType {
public typealias Identity = String
public var identity: Identity { return id }
}
public struct Kid {
var id: String
var name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
public func == (lhs: Kid, rhs: Kid) -> Bool {
return lhs.id == rhs.id
}
extension Kid: Equatable {}
extension Kid: Hashable {
public var hashValue: Int {
return self.id.hashValue
}
}
extension Kid: IdentifiableType {
public typealias Identity = String
public var identity: Identity { return id }
}
extension Kid: Persistable {
public typealias T = NSManagedObject
public static var entityName: String {
return "Parrent"
}
public static var primaryAttributeName: String {
return "id"
}
public init(entity: T) {
self.id = entity.value(forKey: "id") as! String
self.name = entity.value(forKey: "neme") as! String
}
public func update(_ entity: T) {
entity.setValue(self.id, forKey: "id")
entity.setValue(self.name, forKey: "name")
do {
try entity.managedObjectContext?.save()
} catch let e {
print(e)
}
}
}
public extension Reactive where Base: NSManagedObjectContext {
public func create<E: Persistable>(_ type: E.Type = E.self) -> E.T {
return NSEntityDescription.insertNewObject(forEntityName: E.entityName, into: self.base) as! E.T
}
public func get<P: Persistable>(_ persistable: P) throws -> P.T? {
let fetchRequest: NSFetchRequest<P.T> = NSFetchRequest(entityName: P.entityName)
fetchRequest.predicate = NSPredicate(format: "%K = %@", P.primaryAttributeName, persistable.identity)
let result = (try self.base.execute(fetchRequest)) as! NSAsynchronousFetchResult<P.T>
return result.finalResult?.first
}
public func getOrCreateEntity<K: Persistable>(for persistable: K) -> K.T {
if let reusedEntity = try? self.get(persistable), reusedEntity != nil {
return reusedEntity!
} else {
return self.create(K.self)
}
}
}
If those get and create functions were public in this library you wouldn't have to reimplement them, but again I'm not the creator of this library and I'm not sure if this is intended way how to do this.
from rxcoredata.
@CezaryKopacz actually yes, tomorrow i will post it here...and i will create pull request :)
from rxcoredata.
I'm doing a similar thing on a project and I like your answer, @beretis. Have you tried any other variations of your solution?
I won't be starting implementing CoreData until the next week, but I'll start by using your example and will report back with my findings 👍
I'm not sure as to why the 'create'-method is private, but your take on 'getOrCreateEntity' seems to make sense in this context.
from rxcoredata.
@bstien I'm constantly trying to improve this solution and to create some good API for this, then I will create Pull Request...Right now, I'm struggling little bit with functionality. When I ask myself why do you want to use Structs and then transform them into NSManagedObjects? The main reason coming to my mind is because you can easily init them without any context, move them around and then save them to context anytime you want. Now I'm implementing structs that will wrap relationships, but it needs to provide nice API and it still needs to support non coreData data store. Not sure if you know what I mean, I would be happy to chat about this if you are interested.
from rxcoredata.
@beretis any news with that?
from rxcoredata.
@beretis I'm trying to implement your solution, and looking for automatic generation with mogenerator.. :) But I'm also working on a MVVM, did you have try this?
cheers
from rxcoredata.
Has there been any progress with this, or are there any ideas around best practice?
from rxcoredata.
@rossrossp I forked this repo and implemented relationships into it with faulting but at the end there was always some problem. I ended up using just reactive FetchResultController and completely ditched Persistable protocol.
from rxcoredata.
Hi Guys,
have you any new idea? I the same problem, and i want understand to better my code!!! Now I'm using the first solution of @beretis .
J
from rxcoredata.
Related Issues (20)
- AnonymousDisposable: unresolved indentifier HOT 2
- The demo content is few
- create + get private to public
- Create swift4.0 branch HOT 1
- Pod trunk HOT 9
- Stewardship HOT 3
- Podspec doesn't support the Mac. HOT 1
- rx.entities.asSingle() won't have a response to subscribe HOT 1
- [iOS 12] RXCoreData crash on iOS 12 HOT 7
- Does keeping a reference to Observer cause a retain-cycle? HOT 2
- Needs documentation HOT 1
- Extract Persistable
- Use of NSFetchResultController? HOT 2
- Crash on iOS 12 + HOT 1
- Lower deployment target HOT 7
- SPM Support HOT 1
- RxSwift 6.0 support HOT 2
- Proposal for FetchedResultsControllerEntityObserver and FetchedResultsControllerSectionObserver HOT 4
- Swift 3 support HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxcoredata.