Open andrii-serohin opened 7 years ago
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.
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.
@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.
@beretis any news with that?
@CezaryKopacz actually yes, tomorrow i will post it here...and i will create pull request :)
@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
Has there been any progress with this, or are there any ideas around best practice?
@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.
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
Hello. I have some problems with the addition of relations RxCoreData. I think it would be nice if you add some examples to show how to do it.