1

I am using Siesta to fetch resources from my server. And I am following the user guide to configure a transformer to automatically transform JSON strings into Realm objects.

It works for single objects, but somehow, fails with arrays of such objects.

Here is my stripped-down model:

public class ObservingSite: Object {
  dynamic var uuid: String = ""
  dynamic var name: String = ""
}

And the siesta transformer, as in the example, in my Service subclass:

    self.configureTransformer("/\(self.APIVersion)/observingsites/") {
      JSON($0.content as AnyObject).arrayValue.map { ObservingSite(value: $0) }
    }

JSON being a datatype defined by SwiftyJSON.

And the crazy error message I get:

2017-01-22 20:04:00.848504 Arcsecond Demo macOS[25453:8681143] [General] An uncaught exception was raised

2017-01-22 20:04:00.848592 Arcsecond Demo macOS[25453:8681143]     [General] Invalid value '{
  "name" : "Adelaide Observatory",
  "uuid" : "99b98d50-d831-48c2-89b3-2ff6e4c9f4aa"
}' to initialize object of type 'ObservingSite': missing key 'uuid'
2017-01-22 20:04:00.849000 Arcsecond Demo macOS[25453:8681143] [General] (
    0   CoreFoundation                      0x00007fff7fa49e7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff94634cad objc_exception_throw + 48
    2   Realm                               0x000000010040c3f3 RLMValidatedValueForProperty + 282
    3   Realm                               0x000000010040b863 -[RLMObjectBase initWithValue:schema:] + 1105
    4   RealmSwift                          0x00000001002aa5da _TFC10RealmSwift6ObjectcfT5valueP__S0_ + 202
    5   Arcsecond                           0x000000010024fbfb _TFC9Arcsecond13ObservingSitecfT5valueP__S0_ + 203
    6   Arcsecond                           0x000000010024fc84 _TFC9Arcsecond13ObservingSiteCfT5valueP__S0_ + 52
    7   Arcsecond                           0x0000000100253a1c 

But the damn uuid key is here!? What I am missing? Thanks a lot for any help.

onekiloparsec
  • 2,013
  • 21
  • 32

1 Answers1

2

I managed to make it work, without entirely understanding why. But the transformer is actually wrong, and that is not a Realm problem. Now, the Siesta transformer looks like this:

    self.configureTransformer("/\(self.APIVersion)/observingsites/") {
        ($0.content as [AnyObject]).map { ObservingSite(value: $0) }
    }

I basically removed the JSON init.

onekiloparsec
  • 2,013
  • 21
  • 32