RLMSupport.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Realm
  19. extension RLMRealm {
  20. @nonobjc public class func schemaVersion(at url: URL, usingEncryptionKey key: Data? = nil) throws -> UInt64 {
  21. var error: NSError?
  22. let version = __schemaVersion(at: url, encryptionKey: key, error: &error)
  23. guard version != RLMNotVersioned else { throw error! }
  24. return version
  25. }
  26. @nonobjc public func resolve<Confined>(reference: RLMThreadSafeReference<Confined>) -> Confined? {
  27. return __resolve(reference as! RLMThreadSafeReference<RLMThreadConfined>) as! Confined?
  28. }
  29. }
  30. extension RLMObject {
  31. // Swift query convenience functions
  32. public class func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<RLMObject> {
  33. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  34. }
  35. public class func objects(in realm: RLMRealm,
  36. where predicateFormat: String,
  37. _ args: CVarArg...) -> RLMResults<RLMObject> {
  38. return objects(in: realm, with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  39. }
  40. }
  41. public struct RLMIterator<T>: IteratorProtocol {
  42. private var iteratorBase: NSFastEnumerationIterator
  43. internal init(collection: RLMCollection) {
  44. iteratorBase = NSFastEnumerationIterator(collection)
  45. }
  46. public mutating func next() -> T? {
  47. return iteratorBase.next() as! T?
  48. }
  49. }
  50. // Sequence conformance for RLMArray and RLMResults is provided by RLMCollection's
  51. // `makeIterator()` implementation.
  52. extension RLMArray: Sequence {}
  53. extension RLMResults: Sequence {}
  54. extension RLMCollection {
  55. // Support Sequence-style enumeration
  56. public func makeIterator() -> RLMIterator<RLMObject> {
  57. return RLMIterator(collection: self)
  58. }
  59. }
  60. extension RLMCollection {
  61. // Swift query convenience functions
  62. public func indexOfObject(where predicateFormat: String, _ args: CVarArg...) -> UInt {
  63. return indexOfObject(with: NSPredicate(format: predicateFormat, arguments: getVaList(args)))
  64. }
  65. public func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<NSObject> {
  66. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<NSObject>
  67. }
  68. }
  69. // MARK: - Sync-related
  70. #if REALM_ENABLE_SYNC
  71. extension RLMSyncManager {
  72. public static var shared: RLMSyncManager {
  73. return __shared()
  74. }
  75. }
  76. extension RLMSyncUser {
  77. public static var current: RLMSyncUser? {
  78. return __current()
  79. }
  80. public static var all: [String: RLMSyncUser] {
  81. return __allUsers()
  82. }
  83. @nonobjc public var errorHandler: RLMUserErrorReportingBlock? {
  84. get {
  85. return __errorHandler
  86. }
  87. set {
  88. __errorHandler = newValue
  89. }
  90. }
  91. public static func logIn(with credentials: RLMSyncCredentials,
  92. server authServerURL: URL,
  93. timeout: TimeInterval = 30,
  94. callbackQueue queue: DispatchQueue = DispatchQueue.main,
  95. onCompletion completion: @escaping RLMUserCompletionBlock) {
  96. return __logIn(with: credentials,
  97. authServerURL: authServerURL,
  98. timeout: timeout,
  99. callbackQueue: queue,
  100. onCompletion: completion)
  101. }
  102. public func configuration(realmURL: URL? = nil, fullSynchronization: Bool = false,
  103. enableSSLValidation: Bool = true, urlPrefix: String? = nil) -> RLMRealmConfiguration {
  104. return self.__configuration(with: realmURL,
  105. fullSynchronization: fullSynchronization,
  106. enableSSLValidation: enableSSLValidation,
  107. urlPrefix: urlPrefix)
  108. }
  109. }
  110. extension RLMSyncSession {
  111. public func addProgressNotification(for direction: RLMSyncProgressDirection,
  112. mode: RLMSyncProgressMode,
  113. block: @escaping RLMProgressNotificationBlock) -> RLMProgressNotificationToken? {
  114. return __addProgressNotification(for: direction,
  115. mode: mode,
  116. block: block)
  117. }
  118. }
  119. #endif
  120. extension RLMNotificationToken {
  121. @available(*, unavailable, renamed: "invalidate()")
  122. @nonobjc public func stop() { fatalError() }
  123. }