Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. //
  2. // parseViewController.swift
  3. // parseTest
  4. //
  5. // Created by Jay Liew on 6/23/17.
  6. // Copyright © 2017 Jay Liew. All rights reserved.
  7. //
  8.  
  9. // .______ ___ .______ _______. _______
  10. // | _ \ / \ | _ \ / || ____|
  11. // | |_) | / ^ \ | |_) | | (----`| |__
  12. // | ___/ / /_\ \ | / \ \ | __|
  13. // | | / _____ \ | |\ \----.----) | | |____
  14. // | _| /__/ \__\ | _| `._____|_______/ |_______|
  15. //
  16. // ______ __ __ _______ ___ .___________.
  17. // / || | | | | ____| / \ | |
  18. // | ,----'| |__| | | |__ / ^ \ `---| |----`
  19. // | | | __ | | __| / /_\ \ | |
  20. // | `----.| | | | | |____ / _____ \ | |
  21. // \______||__| |__| |_______/__/ \__\ |__|
  22. //
  23. // _______. __ __ _______ _______ .___________.
  24. // / || | | | | ____|| ____|| |
  25. // | (----`| |__| | | |__ | |__ `---| |----`
  26. // \ \ | __ | | __| | __| | |
  27. // .----) | | | | | | |____ | |____ | |
  28. // |_______/ |__| |__| |_______||_______| |__|
  29. //
  30.  
  31. import UIKit
  32. import Parse
  33.  
  34. class parseViewController: UIViewController {
  35.  
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38.  
  39. queryRelationalData()
  40.  
  41. } // viewDidLoad
  42.  
  43. func queryRelationalData(){
  44. // retriving relational data (not pointer, but "relation")
  45.  
  46. let query = PFQuery(className:"ThoughtRecord")
  47. query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
  48. if let thoughtRecords = objects{
  49. for record in thoughtRecords{
  50. print("entered thought records for loop")
  51.  
  52. let relation = record.relation(forKey: "supportingFacts")
  53. let query = relation.query()
  54.  
  55. query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
  56. if let sFacts = objects {
  57. for sFact in sFacts{
  58. print("entered sFact for loop")
  59. print("\(sFact["fact"])")
  60. }
  61. }else{
  62. print("no sfacts")
  63. }
  64. if let error = error {
  65. print("error: \(error.localizedDescription)")
  66. }else{
  67. print("no error")
  68. }
  69.  
  70. })
  71. }
  72. }else{
  73. print("no thought records")
  74. }
  75. } // find in bg
  76. } // queryRelationalData
  77.  
  78. func saveRelationalData(){
  79. // saving relational data (not pointer, but "relation" data type--a Parse contruct)
  80. // an s-fact points to one thought record. One thought record can have many s-facts
  81.  
  82. let sFact = PFObject(className:"SupportingFact")
  83. sFact["fact"] = "lol"
  84.  
  85. let sFact2 = PFObject(className:"SupportingFact")
  86. sFact2["fact"] = "nyan"
  87.  
  88. let sFact3 = PFObject(className:"SupportingFact")
  89. sFact3["fact"] = "cat"
  90.  
  91. let thoughtRecord = PFObject(className:"ThoughtRecord")
  92. thoughtRecord["situation"] = "playa!"
  93.  
  94. let relation = thoughtRecord.relation(forKey: "supportingFacts")
  95. relation.add(sFact)
  96. relation.add(sFact2)
  97. relation.add(sFact3)
  98.  
  99. Timer.scheduledTimer(withTimeInterval: 0.0, repeats: false) { (_) in
  100. // sFacts must be saved FIRST
  101. sFact.saveInBackground { (_ Bool, e:Error?) in
  102. }
  103. sFact2.saveInBackground { (_ Bool, e:Error?) in
  104. }
  105. sFact3.saveInBackground { (_ Bool, e:Error?) in
  106. }
  107. }
  108.  
  109. Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (_) in
  110. // time to make sure sFact objects save before saving the
  111. // thought record, which is the object with the PFRelation field
  112.  
  113. thoughtRecord.saveInBackground { (success:Bool, e:Error?) in
  114. if success == true{
  115. print("record save success")
  116.  
  117. // this relation object is explicitly needed (the one declared above outside this block is not the same)
  118. let relation = thoughtRecord.relation(forKey: "supportingFacts")
  119. let query = relation.query()
  120. query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
  121. if let sFacts = objects {
  122. for sFact in sFacts{
  123. print("sFact for loop")
  124. print("\(sFact["fact"])")
  125. }
  126. }else{
  127. print("no sfacts")
  128. }
  129. if let error = error {
  130. print("error: \(error.localizedDescription)")
  131. }else{
  132. print("no error")
  133. }
  134. }) // find in bg
  135. }else{
  136. print("record save fail")
  137. }
  138. }
  139. }
  140. } // saveRelationalData
  141.  
  142. func queryForObjects(){
  143. // query for objects
  144.  
  145. let query = PFQuery(className:"SupportingFact")
  146. //query.whereKey("playerName", equalTo:"Sean Plott")
  147.  
  148. query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
  149. if let objects = objects{
  150. for sFact in objects{
  151.  
  152. print("\(sFact["fact"]!)")
  153. }
  154. }
  155. }
  156. } // queryForObjects
  157.  
  158. func saveAnObject(){
  159. // save object
  160. let sFact = PFObject(className:"SupportingFact")
  161. sFact["fact"] = "Luvin Cattania"
  162. sFact.saveInBackground { (success: Bool, error: Error?) in
  163. if(success==true){
  164. print("success")
  165. }else{
  166. print("fail")
  167. }
  168. if let error = error{
  169. print("error: \(error.localizedDescription)")
  170. }else{
  171. print("no error obj")
  172. }
  173. } // saveInBackground
  174. } // saveAnObject
  175.  
  176. } // class parseViewController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement