RLMSupport.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #if swift(>=3.2)
  27. @nonobjc public func resolve<Confined>(reference: RLMThreadSafeReference<Confined>) -> Confined? {
  28. return __resolve(reference as! RLMThreadSafeReference<RLMThreadConfined>) as! Confined?
  29. }
  30. #else
  31. @nonobjc public func resolve<Confined: RLMThreadConfined>(reference: RLMThreadSafeReference<Confined>) -> Confined? {
  32. return __resolve(reference as! RLMThreadSafeReference<RLMThreadConfined>) as! Confined?
  33. }
  34. #endif
  35. }
  36. extension RLMObject {
  37. // Swift query convenience functions
  38. public class func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<RLMObject> {
  39. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  40. }
  41. public class func objects(in realm: RLMRealm,
  42. where predicateFormat: String,
  43. _ args: CVarArg...) -> RLMResults<RLMObject> {
  44. return objects(in: realm, with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  45. }
  46. }
  47. public struct RLMIterator<T>: IteratorProtocol {
  48. private var iteratorBase: NSFastEnumerationIterator
  49. internal init(collection: RLMCollection) {
  50. iteratorBase = NSFastEnumerationIterator(collection)
  51. }
  52. public mutating func next() -> T? {
  53. return iteratorBase.next() as! T?
  54. }
  55. }
  56. // Sequence conformance for RLMArray and RLMResults is provided by RLMCollection's
  57. // `makeIterator()` implementation.
  58. extension RLMArray: Sequence {}
  59. extension RLMResults: Sequence {}
  60. extension RLMCollection {
  61. // Support Sequence-style enumeration
  62. public func makeIterator() -> RLMIterator<RLMObject> {
  63. return RLMIterator(collection: self)
  64. }
  65. }
  66. extension RLMCollection {
  67. // Swift query convenience functions
  68. public func indexOfObject(where predicateFormat: String, _ args: CVarArg...) -> UInt {
  69. return indexOfObject(with: NSPredicate(format: predicateFormat, arguments: getVaList(args)))
  70. }
  71. public func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<NSObject> {
  72. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<NSObject>
  73. }
  74. }
  75. // MARK: - Sync-related
  76. extension RLMSyncManager {
  77. public static var shared: RLMSyncManager {
  78. return __shared()
  79. }
  80. }
  81. extension RLMSyncUser {
  82. public static var current: RLMSyncUser? {
  83. return __current()
  84. }
  85. public static var all: [String: RLMSyncUser] {
  86. return __allUsers()
  87. }
  88. @nonobjc public var errorHandler: RLMUserErrorReportingBlock? {
  89. get {
  90. return __errorHandler
  91. }
  92. set {
  93. __errorHandler = newValue
  94. }
  95. }
  96. public static func logIn(with credentials: RLMSyncCredentials,
  97. server authServerURL: URL,
  98. timeout: TimeInterval = 30,
  99. callbackQueue queue: DispatchQueue = DispatchQueue.main,
  100. onCompletion completion: @escaping RLMUserCompletionBlock) {
  101. return __logIn(with: credentials,
  102. authServerURL: authServerURL,
  103. timeout: timeout,
  104. callbackQueue: queue,
  105. onCompletion: completion)
  106. }
  107. public func configuration(realmURL: URL? = nil, fullSynchronization: Bool = false,
  108. enableSSLValidation: Bool = true, urlPrefix: String? = nil) -> RLMRealmConfiguration {
  109. return self.__configuration(with: realmURL,
  110. fullSynchronization: fullSynchronization,
  111. enableSSLValidation: enableSSLValidation,
  112. urlPrefix: urlPrefix)
  113. }
  114. }
  115. extension RLMSyncSession {
  116. public func addProgressNotification(for direction: RLMSyncProgressDirection,
  117. mode: RLMSyncProgressMode,
  118. block: @escaping RLMProgressNotificationBlock) -> RLMProgressNotificationToken? {
  119. return __addProgressNotification(for: direction,
  120. mode: mode,
  121. block: block)
  122. }
  123. }
  124. extension RLMNotificationToken {
  125. @available(*, unavailable, renamed: "invalidate()")
  126. @nonobjc public func stop() { fatalError() }
  127. }