Update from JSON an already saved object. #41

Closed
opened 2025-12-29 15:22:52 +01:00 by adam · 4 comments
Owner

Originally created by @minuscorp on GitHub (Feb 18, 2016).

I'd like to know if its possible given a query object with some properties: let object = CoreStore... I can update it self through the ImportableObject protocol, which has the mapping for the JSON but to a certain already saved object (several backends gives the primary key in respose after you upload a local created object to the backend).

Also, is there a way to export an object to [String: AnyObject] that also encodes their relationships?

Thank you! Grat job with this repo! 👍

Originally created by @minuscorp on GitHub (Feb 18, 2016). I'd like to know if its possible given a query object with some properties: `let object = CoreStore...` I can update it self through the `ImportableObject` protocol, which has the mapping for the JSON but to a certain already saved object (several backends gives the primary key in respose after you upload a local created object to the backend). Also, is there a way to export an object to [String: AnyObject] that also encodes their relationships? Thank you! Grat job with this repo! :+1:
adam added the question label 2025-12-29 15:22:52 +01:00
adam closed this issue 2025-12-29 15:22:52 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 19, 2016):

You can call the ImportableObject methods directly if you want to:

CoreStore.beginAsynchronous { transaction in

    if let object = transaction.fetchOne(From(...), Where(...))
        where object.shouldInsertFromImportSource(json, inTransaction: transaction) {

        _ = try? object.didInsertFromImportSource(json, inTransaction: transaction)
    }
    transaction.commit { ... }
}

It might be more convenient to let the transaction have a method for this though. Feel free to post a github issue for the feature request.

is there a way to export an object to [String: AnyObject] that also encodes their relationships?

The ImportableObject and ImportableUniqueObject protocol methods accept a transaction argument for this very purpose. You can fetch and import from within those methods:

func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws {

    self.attribute1 = source["key1"] as? String
    self.attribute2 = source["key2"] as? NSNumber

    self.relatedObject = try! transaction.importObject(
        Into(RelatedObjectEntity), 
        source: source["relatedObjectKey"] as? RelatedObjectEntity.ImportSource
    )
}
@JohnEstropia commented on GitHub (Feb 19, 2016): You can call the `ImportableObject` methods directly if you want to: ``` swift CoreStore.beginAsynchronous { transaction in if let object = transaction.fetchOne(From(...), Where(...)) where object.shouldInsertFromImportSource(json, inTransaction: transaction) { _ = try? object.didInsertFromImportSource(json, inTransaction: transaction) } transaction.commit { ... } } ``` It might be more convenient to let the transaction have a method for this though. Feel free to post a github issue for the feature request. > is there a way to export an object to [String: AnyObject] that also encodes their relationships? The `ImportableObject` and `ImportableUniqueObject` protocol methods accept a `transaction` argument for this very purpose. You can fetch and import from within those methods: ``` swift func didInsertFromImportSource(source: ImportSource, inTransaction transaction: BaseDataTransaction) throws { self.attribute1 = source["key1"] as? String self.attribute2 = source["key2"] as? NSNumber self.relatedObject = try! transaction.importObject( Into(RelatedObjectEntity), source: source["relatedObjectKey"] as? RelatedObjectEntity.ImportSource ) } ```
Author
Owner

@minuscorp commented on GitHub (Feb 19, 2016):

With the second question I meant if there's a method to export an object to JSON format (a simple [String: AnyObject] that follows the same rules as the exposed in the ImportableObject protocol but in the inverse path)

@minuscorp commented on GitHub (Feb 19, 2016): With the second question I meant if there's a method to export an object to JSON format (a simple `[String: AnyObject]` that follows the same rules as the exposed in the `ImportableObject` protocol but in the inverse path)
Author
Owner

@JohnEstropia commented on GitHub (Feb 19, 2016):

@minuscorp Oh sorry.. For some reason I read that wrong.
I think it's not in CoreStore's scope to implement export utilities. In the end it would end up as a floating protocol that is used nowhere else in CoreStore. I suggest you just implement it directly on your NSManagedObject subclass, or write an extension that uses your app's mapping rules.

@JohnEstropia commented on GitHub (Feb 19, 2016): @minuscorp Oh sorry.. For some reason I read that wrong. I think it's not in CoreStore's scope to implement export utilities. In the end it would end up as a floating protocol that is used nowhere else in CoreStore. I suggest you just implement it directly on your NSManagedObject subclass, or write an extension that uses your app's mapping rules.
Author
Owner

@JohnEstropia commented on GitHub (Feb 20, 2016):

Closing this issue for now. If you have further questions feel free to message anytime :)

@JohnEstropia commented on GitHub (Feb 20, 2016): Closing this issue for now. If you have further questions feel free to message anytime :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#41