-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseManager.swift
151 lines (132 loc) · 4.51 KB
/
ParseManager.swift
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ParseManager.swift
// Pacer
//
// Created by Calvin on 5/11/15.
// Copyright (c) 2015 Joseph Zhong. All rights reserved.
//
import Foundation
import Parse
import Bolts
class ParseManager
{
//these are global variables because of the block execution sync problem where code executes before the array is populated. These eliminates returns
var temparray : [PFObject] = [PFObject]()
var teamarray : [Team] = [Team]()
var teamNames:[String] = []
var compArray : [Competition] = [Competition]()
//swift has weird stuff. Can't just do a static class (wtf apple)
init()
{}
/**
* pulls a player object from the cloud
* @param id the object id of the PFobject
* @return the player object
*/
func pullPlayer(id : String) -> Player
{
return Player(player: pullObject(id, type: "Player"))
}
/**
* pulls a team objectfrom the cloud
* @param id the object id of the PFobject
* @return the team object
*/
func pullTeam(id : String) -> Team
{
return Team(team : pullObject(id, type: "Team"))
}
/**
* pulls a competition off of the cloud
* @param id the object id of the PFobject
* @return the competition object
*/
func pullComp(id : String) -> Competition
{
return Competition(Competition: pullObject(id, type: "Competition"))
}
/**
* pulls all of the teams off of the cloud and stores them in the teamarray
* @param completionHandler wraps the method in a call back making sure that the networking block methods execute in the correct order
*/
func pullTeams(completionHandler: (Bool!, NSError!) -> Void)
{
self.teamarray = []
self.teamNames = []
var completed = false
pullAllObjects("Team", completionHandler: {
(success: Bool!, error: NSError!) -> Void in
if success == true
{
for o in self.temparray
{
self.teamarray.append(Team(team: o))
self.teamNames.append(o["name"] as! String)
}
completionHandler(true, nil)
}
})
}
func pullComps(completionHandler: (Bool!, NSError!) -> Void)
{
self.teamarray = []
self.teamNames = []
var completed = false
pullAllObjects("Competition", completionHandler: {
(success: Bool!, error: NSError!) -> Void in
if success == true
{
for o in self.temparray
{
self.compArray.append(Competition(Competition: o))
}
completionHandler(true, nil)
}
})
}
/**
* private function to pull a single object off of the cloud
* @param id the object id of the PFobject
* @param type the class the object is to be pulled from
* @return the pfobject
*/
func pullObject(id : String, type : String) -> PFObject
{
//var tempobj : PFObject = PFObject()
var query = PFQuery(className: type)
return query.getObjectWithId(id)!
}
/**
* pulls all objects of class type from the cloud and puts them in the temp array
* @param type the class of objects to be pulled
* @param completionHandler the wrapper to help methods execute in the correct order
*/
private func pullAllObjects(type : String, completionHandler: (Bool!, NSError!) -> Void)
{
var query = PFQuery(className: type)
//gets first 100 objects
if(type == "Team")
{
query.orderByDescending("score")
}
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) Teams.")
// Do something with the found objects
//println(objects)
if let objectarray : [PFObject] = objects as? [PFObject] {
//println(objectarray)
self.temparray = objectarray
//println(temparray)
completionHandler(true, nil)
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
completionHandler(false, nil)
}
}
//println("I EXITED")
}
}