Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combination with Core Data #51

Open
LiborZ opened this issue Nov 5, 2015 · 5 comments
Open

Combination with Core Data #51

LiborZ opened this issue Nov 5, 2015 · 5 comments

Comments

@LiborZ
Copy link

LiborZ commented Nov 5, 2015

Is it possible to combine this great object mapping library with Core Data? How should swift class look like? Thanks

@Anviking
Copy link
Owner

Anviking commented Nov 6, 2015

Haven't used Core Data in a long, long time, but could something like this work?

class Test: NSManagedObject {
    @NSManaged var foo: String

    static func decode(json: AnyObject) throws -> Self {
        let test = self.init()
        test.foo = try json => "test"
    }
}

@Anviking
Copy link
Owner

Anviking commented Nov 6, 2015

Though perhaps your question was on Core Data-best-practice, and if so, I do not know.

@KingOfBrian
Copy link
Contributor

I think the problem is that you can't just call self.init() on an NSManagedObject subclass, you have to call NSEntityDescription.insertNewObjectForEntityForName("MyMO", inManagedObjectContext:context). Passing in the initial context is the ugly part. Some thread local global storage is the only method that I can think of that would work. Once you are in a Managed Object class, you can refer to self.managedObjectContext though.

Any thoughts on another approach? I have a feeling that a curried generic function on NSManagedObjectContext might be able to work, but I'm not sure.

@LiborZ
Copy link
Author

LiborZ commented Jan 5, 2016

I am thinking about something like this:

class Article: NSManagedObject {

  override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
  }

  init(inManagedObjectContext context: NSManagedObjectContext) {
    let entity = NSEntityDescription.entityForName("Article", inManagedObjectContext: context)!
    super.init(entity: entity, insertIntoManagedObjectContext: context)
  }

  static func decode(json: AnyObject, inManagedObjectContext context: NSManagedObjectContext) throws -> Article {
    let article = Article(inManagedObjectContext: context)
    article.foo = try json => "test"
  }
}

I am not sure if it is good aproach. If the init methods could be place to NSManagedObject extension then I think this would be really great.

@KingOfBrian
Copy link
Contributor

A larger problem with CoreData / reference types, is you usually don't want to create new objects every import, but you want to update the state of the existing objects. This would require a proxy / generic import object probably that would contain the existing set of objects, and look them up via some defined primary key. Also, because decode is a class method there would be no pre-defined way to apply a decode to an instance.

I think this may be the end of the line for my Core Data investigation here, but I may take a stab at something. Not sure, but this may be outside of the scope of this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants