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

Problem with NSKeyedArchiver #24

Closed
TheLuckyWan opened this issue Feb 8, 2020 · 5 comments
Closed

Problem with NSKeyedArchiver #24

TheLuckyWan opened this issue Feb 8, 2020 · 5 comments

Comments

@TheLuckyWan
Copy link

Hi,

I'm trying to save a simple model to the Realm, but somehow, the new version using NsKeyedArchiver is throwing an exception.

Error Domain=NSCocoaErrorDomain Code=4866 "The data couldn’t be written because it isn’t in the correct format." UserInfo={NSUnderlyingError=0x60000168e8e0 {Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class '__SwiftValue' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class '__SwiftValue' does not adopt it.}}}

Here's and exemple of the model I'm trying to save.

`struct Model: Realmable, Codable {

var id: String = UUID().uuidString
var name: String = ""
var cat: String = "default"

var type: String = "user"

var dictionary: [Int: [SomeArray]] = [:]

var startDate: Date?
var endDate: Date?

static func primaryKey() -> String? {
    return "id"
}

}`

It will save the model, but the dictionary variable will be nil.

I'm stuck there, I have tried many different solution but I won't work.

Thanks !

@arturdev
Copy link
Collaborator

arturdev commented Feb 8, 2020

Does the problem exist even if you change SomeArray with String?

@TheLuckyWan
Copy link
Author

It's working just fine when changing it to a String.

@TheLuckyWan
Copy link
Author

TheLuckyWan commented Feb 10, 2020

I'm trying to save an object from another model, inside an array, inside that dictionary.

So basically, this is what i'm trying to achieve:

var dictionary: [Int: [DataModel]] = [:] :

struct DataModel: Realmable, Codable {

}

@arturdev
Copy link
Collaborator

Currently saving an array of objects in a dictionary is not supported.
As a workaround, I can suggest you do the following:

  1. Add a new private variable with type: [Int: String] (say var underlyingDic: [Int: String] = [:]
  2. Add your old variable to the ignored list
  3. In the setter of your old variable convert your array of Codables to json string, and assign to the newly created variable
  4. In the getter of your old variable read the json string form the newly created var and convert it back.

@TheLuckyWan
Copy link
Author

Hi !

Sorry for the late answer. I've implemented your idea, and it's working like a charm. Nice workaround that'll do the trick for now.

I hadn't thought about it.

Thanks for your help. I'll let you close the issue if you want to close it, or leave it open for now.

@arturdev arturdev closed this as completed Mar 9, 2020
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

2 participants