Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // parseViewController.swift
- // parseTest
- //
- // Created by Jay Liew on 6/23/17.
- // Copyright © 2017 Jay Liew. All rights reserved.
- //
- // .______ ___ .______ _______. _______
- // | _ \ / \ | _ \ / || ____|
- // | |_) | / ^ \ | |_) | | (----`| |__
- // | ___/ / /_\ \ | / \ \ | __|
- // | | / _____ \ | |\ \----.----) | | |____
- // | _| /__/ \__\ | _| `._____|_______/ |_______|
- //
- // ______ __ __ _______ ___ .___________.
- // / || | | | | ____| / \ | |
- // | ,----'| |__| | | |__ / ^ \ `---| |----`
- // | | | __ | | __| / /_\ \ | |
- // | `----.| | | | | |____ / _____ \ | |
- // \______||__| |__| |_______/__/ \__\ |__|
- //
- // _______. __ __ _______ _______ .___________.
- // / || | | | | ____|| ____|| |
- // | (----`| |__| | | |__ | |__ `---| |----`
- // \ \ | __ | | __| | __| | |
- // .----) | | | | | | |____ | |____ | |
- // |_______/ |__| |__| |_______||_______| |__|
- //
- import UIKit
- import Parse
- class parseViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- queryRelationalData()
- } // viewDidLoad
- func queryRelationalData(){
- // retriving relational data (not pointer, but "relation")
- let query = PFQuery(className:"ThoughtRecord")
- query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
- if let thoughtRecords = objects{
- for record in thoughtRecords{
- print("entered thought records for loop")
- let relation = record.relation(forKey: "supportingFacts")
- let query = relation.query()
- query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
- if let sFacts = objects {
- for sFact in sFacts{
- print("entered sFact for loop")
- print("\(sFact["fact"])")
- }
- }else{
- print("no sfacts")
- }
- if let error = error {
- print("error: \(error.localizedDescription)")
- }else{
- print("no error")
- }
- })
- }
- }else{
- print("no thought records")
- }
- } // find in bg
- } // queryRelationalData
- func saveRelationalData(){
- // saving relational data (not pointer, but "relation" data type--a Parse contruct)
- // an s-fact points to one thought record. One thought record can have many s-facts
- let sFact = PFObject(className:"SupportingFact")
- sFact["fact"] = "lol"
- let sFact2 = PFObject(className:"SupportingFact")
- sFact2["fact"] = "nyan"
- let sFact3 = PFObject(className:"SupportingFact")
- sFact3["fact"] = "cat"
- let thoughtRecord = PFObject(className:"ThoughtRecord")
- thoughtRecord["situation"] = "playa!"
- let relation = thoughtRecord.relation(forKey: "supportingFacts")
- relation.add(sFact)
- relation.add(sFact2)
- relation.add(sFact3)
- Timer.scheduledTimer(withTimeInterval: 0.0, repeats: false) { (_) in
- // sFacts must be saved FIRST
- sFact.saveInBackground { (_ Bool, e:Error?) in
- }
- sFact2.saveInBackground { (_ Bool, e:Error?) in
- }
- sFact3.saveInBackground { (_ Bool, e:Error?) in
- }
- }
- Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (_) in
- // time to make sure sFact objects save before saving the
- // thought record, which is the object with the PFRelation field
- thoughtRecord.saveInBackground { (success:Bool, e:Error?) in
- if success == true{
- print("record save success")
- // this relation object is explicitly needed (the one declared above outside this block is not the same)
- let relation = thoughtRecord.relation(forKey: "supportingFacts")
- let query = relation.query()
- query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
- if let sFacts = objects {
- for sFact in sFacts{
- print("sFact for loop")
- print("\(sFact["fact"])")
- }
- }else{
- print("no sfacts")
- }
- if let error = error {
- print("error: \(error.localizedDescription)")
- }else{
- print("no error")
- }
- }) // find in bg
- }else{
- print("record save fail")
- }
- }
- }
- } // saveRelationalData
- func queryForObjects(){
- // query for objects
- let query = PFQuery(className:"SupportingFact")
- //query.whereKey("playerName", equalTo:"Sean Plott")
- query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
- if let objects = objects{
- for sFact in objects{
- print("\(sFact["fact"]!)")
- }
- }
- }
- } // queryForObjects
- func saveAnObject(){
- // save object
- let sFact = PFObject(className:"SupportingFact")
- sFact["fact"] = "Luvin Cattania"
- sFact.saveInBackground { (success: Bool, error: Error?) in
- if(success==true){
- print("success")
- }else{
- print("fail")
- }
- if let error = error{
- print("error: \(error.localizedDescription)")
- }else{
- print("no error obj")
- }
- } // saveInBackground
- } // saveAnObject
- } // class parseViewController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement