Skip to content

Instantly share code, notes, and snippets.

@own2pwn
Last active April 15, 2018 21:33
Show Gist options
  • Save own2pwn/385367816219f53e1991f18aded55b09 to your computer and use it in GitHub Desktop.
Save own2pwn/385367816219f53e1991f18aded55b09 to your computer and use it in GitHub Desktop.
public final class RealmService {
public var realm: Realm {
return try! Realm()
}
// MARK: - Interface
public func nextID<Model: Object>(for modelType: Model.Type) -> Int {
let className = String(describing: modelType)
//
// Stuck here, can't get realm instance.
//
guard let schema = realm.objects(RealmSchema.self).filter("className = %@", className).first else {
let newModel = RealmSchema(with: className)
save(newModel)
return 0
}
let currentID = schema.nextID
updateAsync(schema, { $0.incrementID() })
return currentID
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment