Skip to content

Instantly share code, notes, and snippets.

@jayliew
Last active June 25, 2017 18:03
Show Gist options
  • Save jayliew/1bd266a3ac293f0cf9c9757eae81ea3c to your computer and use it in GitHub Desktop.
Save jayliew/1bd266a3ac293f0cf9c9757eae81ea3c to your computer and use it in GitHub Desktop.
Parse server cheat sheet
//
// parseViewController.swift
// parseTest
//
// Created by Jay Liew on 6/23/17.
// Copyright © 2017 Jay Liew. All rights reserved.
//
/*
_____ __ _ __ ____ __
/\ '__`\ /'__`\ /\`'__\/',__\ /'__`\
\ \ \L\ \/\ \L\.\_\ \ \//\__, `\/\ __/
\ \ ,__/\ \__/.\_\\ \_\\/\____/\ \____\
\ \ \/ \/__/\/_/ \/_/ \/___/ \/____/
\ \_\
\/_/
__ __
/\ \ /\ \__
___\ \ \___ __ __ \ \ ,_\
/'___\ \ _ `\ /'__`\ /'__`\ \ \ \/
/\ \__/\ \ \ \ \/\ __//\ \L\.\_\ \ \_
\ \____\\ \_\ \_\ \____\ \__/.\_\\ \__\
\/____/ \/_/\/_/\/____/\/__/\/_/ \/__/
__ __
/\ \ /\ \__
____\ \ \___ __ __\ \ ,_\
/',__\\ \ _ `\ /'__`\ /'__`\ \ \/
/\__, `\\ \ \ \ \/\ __//\ __/\ \ \_
\/\____/ \ \_\ \_\ \____\ \____\\ \__\
\/___/ \/_/\/_/\/____/\/____/ \/__/
*/
import UIKit
import Parse
class parseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
queryRelationalDataDirectionTwo()
} // viewDidLoad
func queryRelationalDataDirectionTwo(){
// retriving relational data (not pointer, but "relation")
// The OTHER direction from queryRelationalDataDirectionOne()
let query = PFQuery(className:"SupportingFact")
var sFacts = [PFObject]()
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let objectArr = objects{
sFacts = objectArr
}
}
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (_) in
// timer bc async call above (yes, I know it's a hack, doh)
print(sFacts[0]["fact"]!)
let query = PFQuery(className:"ThoughtRecord")
query.whereKey("supportingFacts", equalTo: sFacts[0])
query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if let thoughtRecords = objects{
for record in thoughtRecords{
print(record["situation"])
}
}
})
} // timer
} // queryRelationalData
func queryRelationalDataDirectionOne(){
// 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{
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("\(sFact["fact"])")
}
}
if let error = error {
print("error: \(error.localizedDescription)")
}
})
}
}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"] = "Lolzers"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment