AppDelegate.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 UIKit
  19. import RealmSwift
  20. class Dog: Object {
  21. @objc dynamic var name = ""
  22. @objc dynamic var age = 0
  23. }
  24. class Person: Object {
  25. @objc dynamic var name = ""
  26. let dogs = List<Dog>()
  27. }
  28. #if !swift(>=4.2)
  29. extension UIApplication {
  30. typealias LaunchOptionsKey = UIApplicationLaunchOptionsKey
  31. }
  32. #endif
  33. @UIApplicationMain
  34. class AppDelegate: UIResponder, UIApplicationDelegate {
  35. var window: UIWindow?
  36. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
  37. window = UIWindow(frame: UIScreen.main.bounds)
  38. window?.rootViewController = UIViewController()
  39. window?.makeKeyAndVisible()
  40. do {
  41. try FileManager.default.removeItem(at: Realm.Configuration.defaultConfiguration.fileURL!)
  42. } catch {}
  43. // Create a standalone object
  44. let mydog = Dog()
  45. // Set & read properties
  46. mydog.name = "Rex"
  47. mydog.age = 9
  48. print("Name of dog: \(mydog.name)")
  49. // Realms are used to group data together
  50. let realm = try! Realm() // Create realm pointing to default file
  51. // Save your object
  52. realm.beginWrite()
  53. realm.add(mydog)
  54. try! realm.commitWrite()
  55. // Query
  56. let results = realm.objects(Dog.self).filter(NSPredicate(format: "name contains 'x'"))
  57. // Queries are chainable!
  58. let results2 = results.filter("age > 8")
  59. print("Number of dogs: \(results.count)")
  60. print("Dogs older than eight: \(results2.count)")
  61. // Link objects
  62. let person = Person()
  63. person.name = "Tim"
  64. person.dogs.append(mydog)
  65. try! realm.write {
  66. realm.add(person)
  67. }
  68. // Multi-threading
  69. DispatchQueue.global().async {
  70. autoreleasepool {
  71. let otherRealm = try! Realm()
  72. let otherResults = otherRealm.objects(Dog.self).filter(NSPredicate(format: "name contains 'Rex'"))
  73. print("Number of dogs \(otherResults.count)")
  74. }
  75. }
  76. return true
  77. }
  78. }