Not able to update the NSOrderedSet #205

Open
opened 2025-12-29 15:26:36 +01:00 by adam · 2 comments
Owner

Originally created by @praveen-b27 on GitHub (Apr 13, 2018).

Hi Team,
I'm using corestore for a complex data format, which have multiple nested relationships. Can you please help me how to update the NSOrderedSet (One-Many Relationship data type) with the help of core store.

Sample small piece of json:
{
"id": "1",
"name": "test",
"addressInfo": [{
"area": "test1",
"zipcode": "12323"
}, {
"area": "test2",
"zipcode": "12321"
}]
}

Now address info is one to many relationship,
@NSManaged public var id: String?
@NSManaged public var name: String?
@NSManaged public var addressInfo: NSOrderedSet?

I using ImportableUniqueObject for the class "Data",

public static var uniqueIDKeyPath: String {
return #keyPath(Data.id)
}

There's no unique id for the "addressInfo" entity so I'm using ImportableObject for "AddressInfo" class.

So its adding up to the CoreStore with respect to id "1".

Now when I try to insert the data again, Class "Data" is getting updated since it is ImportableUniqueObject. But Class "AddressInfo" is duplicating, inserting again to the core store since its don't have any unique id to update, so now I have 4 row's in my addressInfo table.

Is there any other solution to update the NSOrderedSet which don't have unique id ?


My next doubt, do core store have extension to give the output as dictionary format ? since we are giving the data as a dict [String: Any] to import. When I try to fetch data it comes as a object which is great, but I need a extension to convert the corestore object to dict[String: Any].

Kindly do the needful to overcome these problems.

Originally created by @praveen-b27 on GitHub (Apr 13, 2018). Hi Team, I'm using corestore for a complex data format, which have multiple nested relationships. Can you please help me how to update the NSOrderedSet (One-Many Relationship data type) with the help of core store. Sample small piece of json: { "id": "1", "name": "test", "addressInfo": [{ "area": "test1", "zipcode": "12323" }, { "area": "test2", "zipcode": "12321" }] } Now address info is one to many relationship, @NSManaged public var id: String? @NSManaged public var name: String? @NSManaged public var addressInfo: NSOrderedSet? I using ImportableUniqueObject for the class "Data", public static var uniqueIDKeyPath: String { return #keyPath(Data.id) } There's no unique id for the "addressInfo" entity so I'm using ImportableObject for "AddressInfo" class. So its adding up to the CoreStore with respect to id "1". Now when I try to insert the data again, Class "Data" is getting updated since it is ImportableUniqueObject. But Class "AddressInfo" is duplicating, inserting again to the core store since its don't have any unique id to update, so now I have 4 row's in my addressInfo table. Is there any other solution to update the NSOrderedSet which don't have unique id ? ----------- My next doubt, do core store have extension to give the output as dictionary format ? since we are giving the data as a dict [String: Any] to import. When I try to fetch data it comes as a object which is great, but I need a extension to convert the corestore object to dict[String: Any]. Kindly do the needful to overcome these problems.
adam added the question label 2025-12-29 15:26:36 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Apr 14, 2018):

On question 1:
You are probably adding to addressInfo every time. Just assign to it directly to replace the old NSOrderedSet. When you do it this way, make sure to delete all orphaned AddressInfo at the end of your transaction.

On question 2:
Importing and exporting are not always 1-to-1, so there's not really a useful utility here that cannot be provided by the standard libraries. You can write a simple func toDictionary() -> [String: Any] on your NSManagedObject subclass, or go as far as implementing Swift's Encodable protocol.

@JohnEstropia commented on GitHub (Apr 14, 2018): On question 1: You are probably adding to `addressInfo` every time. Just assign to it directly to replace the old `NSOrderedSet`. When you do it this way, make sure to delete all orphaned `AddressInfo` at the end of your transaction. On question 2: Importing and exporting are not always 1-to-1, so there's not really a useful utility here that cannot be provided by the standard libraries. You can write a simple `func toDictionary() -> [String: Any]` on your `NSManagedObject` subclass, or go as far as implementing Swift's `Encodable` protocol.
Author
Owner

@praveen-b27 commented on GitHub (Apr 15, 2018):

On question 1:

Previsouly: below piece of code calls all the time, so duplication datas found on DB

       let data = transaction.importObjects(Into< AddressInfo >(), sourceArray: data)
       self.test = NSOrderedSet(array: datas)

Currently:

        if let deletableData = self.test?.array as? [AddressInfo] {
            transaction.delete(deletableData)
        }
        do {
            let datas = transaction.importObjects(Into< AddressInfo >(), sourceArray: data)
            self.test = NSOrderedSet(array: datas)
        } catch { }

Can you please add a sample code to delete and assign it directly for the transaction if this is not a right way to do. Because this addressInfo will have many other child items. So I want those child items to be updated in same logic. The piece of code which I currently changed is working fine since the child items delete rule is cascade, I just wanna make sure is there any well optimised way to do the same process.

Thanks for your help.

@praveen-b27 commented on GitHub (Apr 15, 2018): On question 1: **Previsouly: below piece of code calls all the time, so duplication datas found on DB** let data = transaction.importObjects(Into< AddressInfo >(), sourceArray: data) self.test = NSOrderedSet(array: datas) **Currently:** if let deletableData = self.test?.array as? [AddressInfo] { transaction.delete(deletableData) } do { let datas = transaction.importObjects(Into< AddressInfo >(), sourceArray: data) self.test = NSOrderedSet(array: datas) } catch { } Can you please add a sample code to delete and assign it directly for the transaction if this is not a right way to do. Because this addressInfo will have many other child items. So I want those child items to be updated in same logic. The piece of code which I currently changed is working fine since the child items delete rule is cascade, I just wanna make sure is there any well optimised way to do the same process. Thanks for your help.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#205