Code Monkey home page Code Monkey logo

Comments (9)

beretis avatar beretis commented on July 17, 2024 3

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.

beretis avatar beretis commented on July 17, 2024 1

@CezaryKopacz actually yes, tomorrow i will post it here...and i will create pull request :)

from rxcoredata.

bstien avatar bstien commented on July 17, 2024

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.

beretis avatar beretis commented on July 17, 2024

@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.

CezaryKopacz avatar CezaryKopacz commented on July 17, 2024

@beretis any news with that?

from rxcoredata.

Asavarkhul avatar Asavarkhul commented on July 17, 2024

@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.

rossrossp avatar rossrossp commented on July 17, 2024

Has there been any progress with this, or are there any ideas around best practice?

from rxcoredata.

beretis avatar beretis commented on July 17, 2024

@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.

pjcau avatar pjcau commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.