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

Allow filter when pushing a dictionary into createOrUpdateInRealm #1641

Closed
davejlong opened this issue Mar 16, 2015 · 1 comment
Closed

Allow filter when pushing a dictionary into createOrUpdateInRealm #1641

davejlong opened this issue Mar 16, 2015 · 1 comment

Comments

@davejlong
Copy link

I was recently working with some API data to push into Realm data store formatted like the following:

[
  {
    "id": 1234,
    "confirmation": 483,
    locations: [
      {
        "id": 5678,
        "address": "54 Main Street",
        ...
        "arrive_at": "2015-03-12T17:37Z"
      },
      {
        "id": 5679",
        "address": "72 Center Street",
        ...
        "arrive_at": null
      }
    ],
    ...
  },
  ...
]

The JSON is an array of trips for a hired driver. I have models setup in Swift as follows:

class Trip: RLMObject {
  dynamic var id = 0
  dynamic var confirmation = 0
  dynamic var locations = RLMArray(objectClassName: Location.className())
  ...
}

class Location: RLMObject {
  dynamic var id = 0
  dynamic var address: String = ""
  ...
  dynamic var arriveAt: NSDate = NSDate()
  ...
}

I originally had Swift code like the following to save each block from the API:

func parseTrips (trips: JSON) {
  let realm = RLMRealm.defaultRealm()
  realm.begineWriteTransaction()
  for tripJson in trips.arrayValue {
    Trip.createOrUpdateInRealm(realm, withObject: tripJson.dictionaryObject)
  }
  realm.commitWriteTransaction()
}

The code failed because of the arriveAt property on the Location class. I'd like to see a configurator option somewhere, either in RLMObject when it's passed on object or in createOrUpdateInRealm as an argument to cast variables.

Something like:

class Location: RLMObject {
  dynamic var id = 0
  dynamic var address: String = ""
  dynamic var arriveAt: NSDate = NSDate()

  func castInputObject (data: Dictionary) -> Dictionary {
    data["arriveAt"] = NSDate.fromISO8601String(data["arrive_at"])
  }
}
@segiddins
Copy link
Contributor

Hi @davejlong, we already have a couple of issues tracking this feature request, namely #694, #1272, & #1063. In the meantime, you'll either have to pre-process your dictionaries before passing them into createOrUpdateInRealm, or otherwise look into using something like Realm-JSON or RestKit.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants